Вопрос

i am having following trouble with this code.

unhandled exception of type 'System.ArgumentException' occurred in System.Data.Dll

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();
    }
Это было полезно?

Решение

Problem: You have single quote &quot before and after the Database filename .
Solution : you don't need to provide single quote &quot for database file name.so remove the &quot before and after the database filename.

Try This:

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

EDIT :

if your table name is Table you should enclose it in square brackets [] as it is a Reserved word in SQL-Server.

Try This:

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

Solution 3: you need to use while loop to display all values.

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(); 
}

Другие советы

replace &quote with an escaped quote \" in your connection string so that it's

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

The string, as you have it, would only work if it was in a .config file. (The .config files are XML, and &quote is an XML-Encoded representation of a quotation mark.)

you can see this exampl.

but i think that your problem is that you open the connection but you never close connection;

//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;
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top