我想使用sql数据读取器输出的值。修改后我想更新它们。我已经完成了选择部分,但我不知道如何正确使用它们。
#region Get the value from db
using (SqlConnection c = new SqlConnection(connect))
{
c.Open();
using (SqlCommand cm = new SqlCommand(com,c))
{
cm.Parameters.AddWithValue("@nickname", nick);
SqlDataReader s = null;
s = cm.ExecuteReader();
while (s.Read())
{
string oras = Convert.ToString(s["Oras"]);
string judet = Convert.ToString(s["judet"]);
string adresa = Convert.ToString(s["adresa"]);
}
}
c.Close();
}
#endregion
textBox1.Text = oras;
textBox2.Text = judet;
textBox3.Text = adresa;
答案 0 :(得分:0)
你需要添加和SqlCommand,以及Sql Update字符串和SqlParameters:
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
答案 1 :(得分:0)
这是常用的方法:
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
My_Event _event = new My_Event();
if (reader["Id"] != DBNull.Value) { _event.ID = (int)reader["Id"]; }
if (reader["site"] != DBNull.Value) { _event.Site = reader["site"].ToString(); }
if (reader["time"] != DBNull.Value) { _event.Time = (DateTime)reader["time"]; }
_events.AddFirst(_event);
}
reader.Close();
}
检查该值是否为DBNull.Value
,如果不是,则将其转换为原始类型。
答案 2 :(得分:0)
您需要按照以下方式更改代码
#region Get the value from db
using (SqlConnection c = new SqlConnection(connect))
{
c.Open();
using (SqlCommand cm = new SqlCommand(com,c))
{
cm.Parameters.AddWithValue("@nickname", nick);
SqlDataReader s = null;
s = cm.ExecuteReader();
while (s.Read())
{
string oras = Convert.ToString(s["Oras"]);
string judet = Convert.ToString(s["judet"]);
string adresa = Convert.ToString(s["adresa"]);
textBox1.Text = oras;
textBox2.Text = judet;
textBox3.Text = adresa;
}
}
c.Close();
}
#endregion