如何从 Datareader 获取多行
How to get multiple rows from Datareader
这是我的代码。它工作正常,但 returns 只有一行。正如您在 SQL 语句中看到的那样,我需要在表单中使用的数据网格中编辑 2 行 return。当程序读取 2 行时,它只显示一行。 NameAddrmark 是字段的构造函数。
public NameAddrmark GetNameRespCommentData(string respid)
{
NameAddrmark cms = new NameAddrmark();
//var cms = new List<NameAddrmark>();
SqlConnection connection = new SqlConnection(GeneralData.getConnectionString());
string sql = "SELECT top 2 * FROM dbo.RESPONDENT_COMMENT WHERE respid = " + GeneralData.AddSqlQuotes(respid) + " and USRNME = " + GeneralData.AddSqlQuotes(UserInfo.UserName) + " order by COMMDATE ASC";
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
while(reader.Read())
//if (reader.Read())
{
cms.Id = respid;
cms.Date8 = reader["COMMDATE"].ToString();
cms.Usrnme = reader["USRNME"].ToString();
cms.Marktext = reader["COMMTEXT"].ToString();
}
//else
// cms = null;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
return cms;
}
在我的表单上,我 return 向列表发送了 cms。将数据网格的数据源设置为列表。正确显示第一行,但我需要显示两行。希望有人能帮忙。谢谢
你可以return一个List<NameAddrmark>
:
public List<NameAddrmark> GetNameRespCommentData(string respid)
{
List<NameAddrmark> cmsList = new List<NameAddrmark>();
SqlConnection connection = new SqlConnection("insert connection string");
string sql = "SELECT top 2 * FROM dbo.RESPONDENT_COMMENT WHERE respid = " + GeneralData.AddSqlQuotes(respid) + " and USRNME = " + GeneralData.AddSqlQuotes(UserInfo.UserName) + " order by COMMDATE ASC";
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
NameAddrmark cms = new NameAddrmark();
cms.Id = respid;
cms.Date8 = reader["COMMDATE"].ToString();
cms.Usrnme = reader["USRNME"].ToString();
cms.Marktext = reader["COMMTEXT"].ToString();
cmsList.Add(cms);
}
}
catch (SqlException ex)
{
throw; // instead log the exception
}
finally
{
connection.Close();
}
return cmsList;
}
但是你真的应该使用参数而不是字符串连接来防止sql注入。
private void button2_Click(object sender, EventArgs e)
{
//database connection string and opening area
string oracleDb = @"Data Source="connection string here"";
OracleConnection conn = new OracleConnection(oracleDb);
conn.Open();
//declareing paramater and readning parameter input
OracleParameter param = new OracleParameter();
param.OracleDbType = OracleDbType.Decimal;
param.Value = txtlist.Text;
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
//sendting the parameter to the tabase query
cmd.Parameters.Add(param);
cmd.CommandText = "SELECT NAME, ADDRESS FROM FRIENDS WHERE age = :1";
cmd.CommandType = CommandType.Text;
OracleDataReader dataread = cmd.ExecuteReader();
dataread.Read();
if (dataread.HasRows)
{
while (dataread.Read())
{
listBox1.Items.Add(dataread.GetString(1) + " from " + dataread.GetString(2));
}
}
else
{
listBox1.Text = "Not Found";
MessageBox.Show("Data Not found", "NOT FOUND", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
conn.Dispose();
}
这是我的代码。它工作正常,但 returns 只有一行。正如您在 SQL 语句中看到的那样,我需要在表单中使用的数据网格中编辑 2 行 return。当程序读取 2 行时,它只显示一行。 NameAddrmark 是字段的构造函数。
public NameAddrmark GetNameRespCommentData(string respid)
{
NameAddrmark cms = new NameAddrmark();
//var cms = new List<NameAddrmark>();
SqlConnection connection = new SqlConnection(GeneralData.getConnectionString());
string sql = "SELECT top 2 * FROM dbo.RESPONDENT_COMMENT WHERE respid = " + GeneralData.AddSqlQuotes(respid) + " and USRNME = " + GeneralData.AddSqlQuotes(UserInfo.UserName) + " order by COMMDATE ASC";
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
while(reader.Read())
//if (reader.Read())
{
cms.Id = respid;
cms.Date8 = reader["COMMDATE"].ToString();
cms.Usrnme = reader["USRNME"].ToString();
cms.Marktext = reader["COMMTEXT"].ToString();
}
//else
// cms = null;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
return cms;
}
在我的表单上,我 return 向列表发送了 cms。将数据网格的数据源设置为列表。正确显示第一行,但我需要显示两行。希望有人能帮忙。谢谢
你可以return一个List<NameAddrmark>
:
public List<NameAddrmark> GetNameRespCommentData(string respid)
{
List<NameAddrmark> cmsList = new List<NameAddrmark>();
SqlConnection connection = new SqlConnection("insert connection string");
string sql = "SELECT top 2 * FROM dbo.RESPONDENT_COMMENT WHERE respid = " + GeneralData.AddSqlQuotes(respid) + " and USRNME = " + GeneralData.AddSqlQuotes(UserInfo.UserName) + " order by COMMDATE ASC";
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
NameAddrmark cms = new NameAddrmark();
cms.Id = respid;
cms.Date8 = reader["COMMDATE"].ToString();
cms.Usrnme = reader["USRNME"].ToString();
cms.Marktext = reader["COMMTEXT"].ToString();
cmsList.Add(cms);
}
}
catch (SqlException ex)
{
throw; // instead log the exception
}
finally
{
connection.Close();
}
return cmsList;
}
但是你真的应该使用参数而不是字符串连接来防止sql注入。
private void button2_Click(object sender, EventArgs e)
{
//database connection string and opening area
string oracleDb = @"Data Source="connection string here"";
OracleConnection conn = new OracleConnection(oracleDb);
conn.Open();
//declareing paramater and readning parameter input
OracleParameter param = new OracleParameter();
param.OracleDbType = OracleDbType.Decimal;
param.Value = txtlist.Text;
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
//sendting the parameter to the tabase query
cmd.Parameters.Add(param);
cmd.CommandText = "SELECT NAME, ADDRESS FROM FRIENDS WHERE age = :1";
cmd.CommandType = CommandType.Text;
OracleDataReader dataread = cmd.ExecuteReader();
dataread.Read();
if (dataread.HasRows)
{
while (dataread.Read())
{
listBox1.Items.Add(dataread.GetString(1) + " from " + dataread.GetString(2));
}
}
else
{
listBox1.Text = "Not Found";
MessageBox.Show("Data Not found", "NOT FOUND", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
conn.Dispose();
}