试图从控制台应用程序打开SQL连接- conn.open失败

本文关键字:连接 conn open 失败 SQL 控制台 应用程序 | 更新日期: 2023-09-27 18:13:10

我的代码得到conn.Open并给我"抛出异常:'System.Data.SqlClient. 'SqlException' in System.Data.dll"

下面是代码块:

        _timer.Stop();
        string path = @"C:'testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open(); // Dies here
        SqlDataReader rdr = cmd.ExecuteReader();

试图从控制台应用程序打开SQL连接- conn.open失败

订单缺失…首先打开连接,然后使用SQL命令

_timer.Stop();
        string path = @"C:'testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        conn.Open(); // sholud be here
        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataReader rdr = cmd.ExecuteReader();

除此之外,你的代码也没有被格式化,格式如下

 string path = @"C:'testlog.log";
            String connectionString = ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open(); // sholud be here
                string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
                SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                { 
                    String RawImportEnabled = Convert.ToString(reader["RawImportEnabled"]);
                    //Do some thing
                }
            }

这有一些好处,比如通过放置断点调试连接字符串,处理连接而不用担心使用语句等

            _timer.Stop();
        string path = @"C:'testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        conn.Open(); // Dies here again.
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)