Click here to Skip to main content
15,166,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all...

I am writing a windows application to get all the tables of an MDF file which is not already attached to the server but is kept in the hdd.
I am using the following method. I am taking the filename from an openfiledialog box.
But when i try to fill the datatable using the fill method.
I get the exception "keyword not supported .\sqlexpress;attachdbfilename"

One more thing. If I am hard coding the name of the database file I am choosing from openfiledialog then the code is running without any problems.

the code is as follows
C#
openFileDialog1.Filter = "MDF Files| *.mdf";
DialogResult drr = openFileDialog1.ShowDialog();
if (drr == DialogResult.OK)
{
  String db = ".\\SQLEXPRESS;";
  String server = "AttachDbFilename=\"" + openFileDialog1.FileName + "\";";
  String auth = "Integrated Security=True;User Instance=True";
  String cnstr = db + server + auth;
  SqlDataAdapter da = new SqlDataAdapter("select name from sys.tables", cnstr);
  DataTable dttb = new DataTable();
  da.Fill(dttb);
}


I even tried using the @ notation instead of using \". Still the problem persists..
Please help.
Posted
Updated 15-Jul-12 7:47am
v5

Your code has: db = ".\\SQLEXPRESS;"; backslash before SQL Express which is correct but your error says ./sqlexpress;attachdbfilename - a front slash!

It should be like:
C#
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

Refer: Connection String for SQL Server[^]

Make sure that the connection string is correct. Use Visual Studio DEBUGGER and see what is the value of 'cnstr' being passed to adapter.
   
Comments
Rahul_Patel 15-Jul-12 12:56pm
   
thnks a lot sir.... Vaise I had mistyped ./sqlexpress instead of .\sqlexpress
But your connection string worked.
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Instead of
Data Source=.\SQLEXPRESS;AttachDbFilename="C:\abc\DB_Explorer\Database1.mdf";Integrated Security=True;User Instance=True
Rahul_Patel 15-Jul-12 12:56pm
   
+5
Sandeep Mewara 16-Jul-12 2:03am
   
Good to know things are resolved. Welcome.
you should use connection string for

sqlconnection con=new sqlconnection("data source=.;database=--databasename--;integrated security=true");

if your sql required password then
sqlconnection con=new sqlconnection("data source=.;database=--databasename--;uid=sa;pwd=sa");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900