SqlConnection()异常

时间:2013-12-10 17:33:31

标签: c# sqlconnection

我在使用此代码时遇到了问题。

  

System.Data.Dll中发生未处理的“System.ArgumentException”类型异常

static void Main(string[] args)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlCommand cmd = new SqlCommand("Select * from Student", con);
        con.Open();
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Console.WriteLine("{0}", dr[0].ToString());
        }
        Console.ReadKey();
    }

3 个答案:

答案 0 :(得分:2)

问题: &quot之前和之后您有单引号Database filename
解决方案:您无需为数据库文件名提供单引号&quot。请删除&quot之前和之后的database filename

试试这个:

con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

编辑:

如果您的表名是Table,则应将其括在方括号[]中,因为它是SQL-Server中的Reserved word

试试这个:

 SqlCommand cmd =  new SqlCommand("Select * from [Table]",con );

解决方案3:您需要使用while循环显示所有值。

static void Main(string[] args)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlCommand cmd =  new SqlCommand("Select * from [Table]",con );
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine("{0}",dr[0].ToString());
}
Console.ReadKey(); 
}

答案 1 :(得分:1)

在您的连接字符串中使用转义引用&quote替换\",以便

        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=\";C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF\";Integrated Security=True;Connect Timeout=30;User Instance=True";

字符串就像你拥有它一样,只有在.config文件中才有效。 (.config文件是XML,&quote是引号的XML编码表示。)

答案 2 :(得分:0)

你可以看到这个例子。

但我认为您的问题是您打开了连接,但从未关闭连接;

//this is my class  of data or my entity 
public class datos 
    {
        public string column1 { get; set; }
        public string column2 { get; set; }
    }     

   //create a string with the connections parameter
   public static string  myConnection { get { return       @"Server=.\SQLExpress;AttachDbFilename=C:\Users\base.mdf;Trusted_Connection=Yes;"; } }

    //Create a method  of type List<datos> to return a list of datos
public List<datos> example (){   
List<datos> lista = new List<datos>();
      //declare and initialize my entity of type datos
        datos dat = new datos();
        //create a new command to do a query to my database
        SqlCommand adaptador = new SqlCommand("Select * from Yourtable", myConnection);
         //open my connection 
         myConnection.Open();
        // execute my command
         SqlDataReader x =  adaptador.ExecuteReader();
        //now i read the data that i get from my command and add data to my list
         while (x.Read()) 
         {
            dat.column1 = x["column1fromyoutable"].ToString();
            dat.column2 = x["column2fromyourtable"].ToString();
            lista.Add(dat);
         }
        // close connection
         myConnection.Close();
        //return list of data
        return lista;
    }