使用带有存储过程的C#从sql数据库中检索数据

时间:2023-02-11 01:44:50

Whenever I try to retrieve data from my database i keep getting null. The code i'm using is below:

每当我尝试从我的数据库中检索数据时,我都会变为空。我正在使用的代码如下:

protected void Button2_Click(object sender, EventArgs e)
 {
    SqlConnection myConnection = new SqlConnection(GetConnectionString());
    SqlCommand cmd = new SqlCommand("spSelectCustomer", myConnection);
    cmd.CommandType = CommandType.StoredProcedure;
    myConnection.Open();

    SqlParameter custId = cmd.Parameters.Add("@CustomerId", SqlDbType.Int);
    custId.Direction = ParameterDirection.Input;
    custId.Value = 10;

    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    Label1.Text = dr["FirstName"].ToString();
    Label2.Text = dr["LastName"].ToString();
    Label3.Text = dr[3].ToString();
    Label4.Text = dr["Email"].ToString();
}
private static string GetConnectionString()
{
    return ConfigurationManager.ConnectionStrings["Lab3ConnectionString"].ConnectionString;
}

4 个解决方案

#1


7  

You need to call Read before you can access data, Your code should be

您需要在访问数据之前调用Read,您的代码应该是

While (dr.Read())
{

    Label1.Text = dr["FirstName"].ToString();
    Label2.Text = dr["LastName"].ToString();
    Label3.Text = dr[3].ToString();
    Label4.Text = dr["Email"].ToString();
}

//close DataReader
dr.Close();

#2


2  

You are missing a call to Read() on your reader. I also suggest you wrap your IDisposable objects in using statements as below.

您在阅读器上缺少对Read()的调用。我还建议你将IDisposable对象包装在using语句中,如下所示。

You also seem to be using a strange combination of column names and ordinal positions when retrieving your values from the SqlDataReader.

从SqlDataReader检索值时,您似乎也使用了列名和序号位置的奇怪组合。

protected void Button2_Click(object sender, EventArgs e)
{
    using (SqlConnection myConnection = new SqlConnection(GetConnectionString()))
    {
        using (SqlCommand cmd = new SqlCommand("spSelectCustomer", myConnection))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            myConnection.Open();

            SqlParameter custId = cmd.Parameters.AddWithValue("@CustomerId", 10);

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                if (dr.Read())
                {
                    Label1.Text = dr["FirstName"].ToString();
                    Label2.Text = dr["LastName"].ToString();
                    Label3.Text = dr[3].ToString();
                    Label4.Text = dr["Email"].ToString();
                }
            }
        }
    }
}

#3


1  

Before you read column values from DataReader you must invoke Read() method from data reader.

在从DataReader读取列值之前,必须从数据读取器调用Read()方法。

if (dr.Read())
{
    Label1.Text = dr["FirstName"].ToString();
    Label2.Text = dr["LastName"].ToString();
    Label3.Text = dr[3].ToString();
    Label4.Text = dr["Email"].ToString();
}

You can also try:

你也可以尝试:

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);

#4


0  

SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read()) 
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();

//Implicitly closes the connection because CommandBehavior.CloseConnection was specified.

//隐式关闭连接,因为指定了CommandBehavior.CloseConnection。

Documentation : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71).aspx

文档:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v = vs.71).aspx

#1


7  

You need to call Read before you can access data, Your code should be

您需要在访问数据之前调用Read,您的代码应该是

While (dr.Read())
{

    Label1.Text = dr["FirstName"].ToString();
    Label2.Text = dr["LastName"].ToString();
    Label3.Text = dr[3].ToString();
    Label4.Text = dr["Email"].ToString();
}

//close DataReader
dr.Close();

#2


2  

You are missing a call to Read() on your reader. I also suggest you wrap your IDisposable objects in using statements as below.

您在阅读器上缺少对Read()的调用。我还建议你将IDisposable对象包装在using语句中,如下所示。

You also seem to be using a strange combination of column names and ordinal positions when retrieving your values from the SqlDataReader.

从SqlDataReader检索值时,您似乎也使用了列名和序号位置的奇怪组合。

protected void Button2_Click(object sender, EventArgs e)
{
    using (SqlConnection myConnection = new SqlConnection(GetConnectionString()))
    {
        using (SqlCommand cmd = new SqlCommand("spSelectCustomer", myConnection))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            myConnection.Open();

            SqlParameter custId = cmd.Parameters.AddWithValue("@CustomerId", 10);

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                if (dr.Read())
                {
                    Label1.Text = dr["FirstName"].ToString();
                    Label2.Text = dr["LastName"].ToString();
                    Label3.Text = dr[3].ToString();
                    Label4.Text = dr["Email"].ToString();
                }
            }
        }
    }
}

#3


1  

Before you read column values from DataReader you must invoke Read() method from data reader.

在从DataReader读取列值之前,必须从数据读取器调用Read()方法。

if (dr.Read())
{
    Label1.Text = dr["FirstName"].ToString();
    Label2.Text = dr["LastName"].ToString();
    Label3.Text = dr[3].ToString();
    Label4.Text = dr["Email"].ToString();
}

You can also try:

你也可以尝试:

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);

#4


0  

SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read()) 
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();

//Implicitly closes the connection because CommandBehavior.CloseConnection was specified.

//隐式关闭连接,因为指定了CommandBehavior.CloseConnection。

Documentation : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71).aspx

文档:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v = vs.71).aspx