none
ServerVersion = “conn.ServerVersion”引发了类型“System.InvalidOperationException”的异常 RRS feed

  • 问题

  •  string connStr = @"server=.\SQLEXPRESS;database=[MyFirstDB];User ID=sa;pwd=123;";
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();

    ServerVersion = “conn.ServerVersion”引发了类型“System.InvalidOperationException”的异常

    2015年7月26日 12:16

答案

  • 您好,请参考

    https://msdn.microsoft.com/zh-cn/library/system.invalidoperationexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    http://blog.csdn.net/friendan/article/details/7628268


    Love SQL

    2015年7月26日 15:39
  • 您好,

    应该是链接字符串的问题,尝试放在web.config 里呢:

      <connectionStrings>
        <add name="NorthwindConnectionString" connectionString="Data Source=VDI-V-NANY\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=138122c@"    providerName="System.Data.SqlClient"/>
      </connectionStrings>

     
     private DataTable GetData()
            {
                string constr = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                string command = "Select * from Employees where EmployeeID='"+this.CustomerID.Text+"'";
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(command))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }

    2015年8月6日 9:57
    版主

全部回复

  • 您好,请参考

    https://msdn.microsoft.com/zh-cn/library/system.invalidoperationexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    http://blog.csdn.net/friendan/article/details/7628268


    Love SQL

    2015年7月26日 15:39
  • 您好,

    应该是链接字符串的问题,尝试放在web.config 里呢:

      <connectionStrings>
        <add name="NorthwindConnectionString" connectionString="Data Source=VDI-V-NANY\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=138122c@"    providerName="System.Data.SqlClient"/>
      </connectionStrings>

     
     private DataTable GetData()
            {
                string constr = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                string command = "Select * from Employees where EmployeeID='"+this.CustomerID.Text+"'";
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(command))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }

    2015年8月6日 9:57
    版主