C# 如何使用sqldatareader中的值

C# 如何使用sqldatareader中的值,c#,asp.net,sql,sql-server,ado.net,C#,Asp.net,Sql,Sql Server,Ado.net,我想使用sql数据读取器输出的值。修改后,我想更新它们。我已经完成了选择部分,但我不知道如何正确使用它们 #region Get the value from db using (SqlConnection c = new SqlConnection(connect)) { c.Open(); using (SqlCommand cm = new SqlCommand(com,c)) {

我想使用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;

您需要为此添加和SqlCommand以及Sql更新字符串和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();
    }
}

以下是常见的方法:

 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
,如果不是,则将其转换为原始类型。

您必须按以下方式更改代码

#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

我已经这样做了,但它没有给我带来价值。nick是一个全局变量,由。在表格1中,我有global.GlobalVariable=textbox1.text,这里我有nick=global.GlobalVariable。可能是这个问题吗?在这个函数中放置一个断点,然后检查你的nick是否有值。此外,我看到您将参数发送到某处,但发送到何处?您是在尝试执行存储过程,还是在发送sql查询?我在此处发送参数字符串com=@“Select(Oras,Judet,Adresa)from informatii,其中昵称=@昵称”;是的,我知道,但它只是没有先向我展示价值观。nick是使用全局函数的另一种形式的变量。form1:global.globalvalue=textbox1.text,这里我有nick=global.globalvalue可能是这个问题吗?@raulbag1996你应该使用类而不是全局变量。并将业务和数据访问逻辑与视图代码隔离。在此处阅读有关nlayered体系结构的更多信息,例如: