problems connecting to local database
-
Hi, I`m using visual studio 2005. I`ve created a local database "db.mdf", and I`m just trying to connect with, but when I run code it throws an exception: ServerVersion = 'sqlconn.ServerVersion' threw an exception of type 'System.InvalidOperationException' and after that the program stops to respond...What should I do to connect? here is my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace DB { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string c = "Data Source=(local);database=db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; SqlConnection sqlconn = new SqlConnection(c); sqlconn.Open(); string query = "SELECT ASM_VARDAS, ASM_PAVARDE, ASM_GIM_DATA FROM v_ASMENYS WHERE ASM_AK = "; SqlCommand sqlcomm = new SqlCommand(query, sqlconn); SqlDataReader reader = sqlcomm.ExecuteReader(); reader.Read(); } } }
-
Hi, I`m using visual studio 2005. I`ve created a local database "db.mdf", and I`m just trying to connect with, but when I run code it throws an exception: ServerVersion = 'sqlconn.ServerVersion' threw an exception of type 'System.InvalidOperationException' and after that the program stops to respond...What should I do to connect? here is my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace DB { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string c = "Data Source=(local);database=db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; SqlConnection sqlconn = new SqlConnection(c); sqlconn.Open(); string query = "SELECT ASM_VARDAS, ASM_PAVARDE, ASM_GIM_DATA FROM v_ASMENYS WHERE ASM_AK = "; SqlCommand sqlcomm = new SqlCommand(query, sqlconn); SqlDataReader reader = sqlcomm.ExecuteReader(); reader.Read(); } } }
-
You meant 'AttachDbFilename=db.mdf;'?
-
You meant 'AttachDbFilename=db.mdf;'?
-
Shouldn't it be like this:
string c = "Data Source=(local);Initial Catalog=db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
HTHI don`t think it`s a problem with connection string... I`m not able to connect no mathet what I write in that connection string...