Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行需要打开连接的读卡器_C#_Connection String - Fatal编程技术网

C# 执行需要打开连接的读卡器

C# 执行需要打开连接的读卡器,c#,connection-string,C#,Connection String,您好,我一直在为一个项目创建一个订单,但很难将日期记录到数据行中。我有下面的代码,但得到错误“ExecuteReader需要一个开放的可用连接。连接的当前状态为关闭。“。在此方面的任何帮助都将不胜感激 private void cmbSelectDay_SelectedIndexChanged(object sender, EventArgs e) { //variable for case statement int day = 0;

您好,我一直在为一个项目创建一个订单,但很难将日期记录到数据行中。我有下面的代码,但得到错误“ExecuteReader需要一个开放的可用连接。连接的当前状态为关闭。“。在此方面的任何帮助都将不胜感激

 private void cmbSelectDay_SelectedIndexChanged(object sender, EventArgs e)
    {

        //variable for case statement
        int day = 0;

        string Sql = "Select deliveryDayNo from standardOrderDetails order by deliveryDayNo";
        SqlConnection conn = new SqlConnection(connStr);  // ...
        SqlCommand cmd = new SqlCommand(Sql, conn); //there is an error happening here that im not too sure about that would need to be fixed first. but once its fixed this should all work.
        SqlDataReader DR = cmd.ExecuteReader();


            while (DR.Read())
            {
                cmbSelectDay.Items.Add(DR[0]);

            }


                //case statement to take the selected value of the combo box and assign a new value to the variable "day" acordingly.
                switch(cmbSelectDay.SelectedText.ToString())
                {
                    case "Monday":
                        {
                            day = 1;
                            break;
                        }
                    case "Tuesday":
                        {
                            day = 2;
                            break;
                        }
                    case "Wednesday":
                        {
                            day = 3;
                            break;
                        }
                    case "Thursday":
                        {
                            day = 4;
                            break;
                        }
                    case "Friday":
                        {
                            day = 5;
                            break;
                        }
                    case "Saturday":
                        {
                            day = 6;
                            break;
                        }

                }


                // DataRow dr = dsLeprechaun.Tables["standardOrderDetails"].Rows.Find(day);
                DataRow dr = dsLeprechaun.Tables["standardOrderDetails"].Rows.Find(day.ToString());
                dgvOrder.Rows.Add(dr["deliveryDayNo"].ToString());


            lblSelectCategory.Visible = true;
            lstBoxSelectCategory.Visible = true;


    }

在调用
ExecuteReader()
方法之前,需要打开连接对象
conn

试试这个:

SqlConnection conn = new SqlConnection(connStr); 
SqlCommand cmd = new SqlCommand(Sql, conn); 
conn.Open();                                 //Add this statement
SqlDataReader DR = cmd.ExecuteReader();

所以。。。打开连接:

using(SqlConnection conn = new SqlConnection(connStr))
using(SqlCommand cmd = new SqlCommand(Sql, conn))
{
    conn.Open();
    using(SqlDataReader DR = cmd.ExecuteReader())
    {
        // consume data
    }
}
您可能还希望研究有助于ADO.NET的工具,例如“dapper”-例如,如果我假设此数据是字符串,我们可以将所有这些缩短为:

using(SqlConnection conn = new SqlConnection(connStr))
{
    var days = conn.Query<string>(
        "Select deliveryDayNo from standardOrderDetails order by deliveryDayNo"
    ).ToList();
    // now add the items from "days"
}
使用(SqlConnection conn=newsqlconnection(connStr))
{
变量天数=连接查询(
“从standardOrderDetails中选择deliveryDayNo按deliveryDayNo排序”
).ToList();
//现在添加“天”中的项目
}

您必须打开连接

conn.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
  {
    while(reader.Read())
    {
      //code here
    }
  }

嗯,您还没有调用
conn.Open()
来打开连接。。。(您还应该使用
语句,理想情况下使用更高级别的API,但这是另一回事。)还可以使用来处理
SqlConnection
SqlCommand
SqlDataReader
。您好,我添加了连接,代码似乎运行不正常,但我遇到了另一个错误“对象引用未设置为对象的实例。”。