SqlDataReader 打印多行,如何拆分?
SqlDataReader prints multiple rows, how can I split?
我想使用 SqlDataReader
从数据库中检索数据。我让它工作,但我的查询 return 多于一行数据,我想将其分开。
这是我的代码
string query = "select title, rating, dor from movie where title like '%"+name+"%';";
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader[0].ToString());
Response.Write(reader[1].ToString());
Response.Write(reader[2].ToString());
}
}
else
{
Response.Write("No rows found");
}
reader.Close();
}
例如.. reader[0].ToString()
是标题,因为它 return 打印了不止一行数据,所以它打印 "title1title2"。有没有办法拆分它,使第 1 行的标题与第 2 行的标题分开?
试试这个
string query = "select title, rating, dor from movie where title like '%"+name+"%';";
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader[0].ToString());
Response.Write(reader[1].ToString());
Response.Write(reader[2].ToString());
Response.Write("------------------------");//whatever separator you want to use
}
}
else
{
Response.Write("No rows found");
}
reader.Close();
}
我想使用 SqlDataReader
从数据库中检索数据。我让它工作,但我的查询 return 多于一行数据,我想将其分开。
这是我的代码
string query = "select title, rating, dor from movie where title like '%"+name+"%';";
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader[0].ToString());
Response.Write(reader[1].ToString());
Response.Write(reader[2].ToString());
}
}
else
{
Response.Write("No rows found");
}
reader.Close();
}
例如.. reader[0].ToString()
是标题,因为它 return 打印了不止一行数据,所以它打印 "title1title2"。有没有办法拆分它,使第 1 行的标题与第 2 行的标题分开?
试试这个
string query = "select title, rating, dor from movie where title like '%"+name+"%';";
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader[0].ToString());
Response.Write(reader[1].ToString());
Response.Write(reader[2].ToString());
Response.Write("------------------------");//whatever separator you want to use
}
}
else
{
Response.Write("No rows found");
}
reader.Close();
}