如何从Datareader获取多行
本文关键字:获取 Datareader | 更新日期: 2023-09-27 18:21:45
这是我的代码。它工作正常,但只返回一行。正如您在SQL语句中看到的,我需要在表单中使用的数据网格中返回2行。当过程读取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;
}
在我的表格上,我将cms返回到列表中。将数据网格的数据源设置为列表。正确显示第一行,但我需要显示两行。希望有人能帮忙。谢谢
您可以返回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();
}