Run a SQL file on .MDF file using C# -


i have got .mdf file @ specific location on want run .sql file. using following code doesn't well.

it gives me error failed connect server , cannot read physical file!

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms;  using system; using system.data.sqlclient; using system.io; using microsoft.sqlserver.management.common; using microsoft.sqlserver.management.smo;  namespace testing {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }     private void btncreatedatabase_click(object sender, eventargs e)     {         string dir = getparentpath() + "\\database";  // folder location         if (!directory.exists(dir))             directory.createdirectory(dir);          file.create(getparentpath() + "\\database\\test_db.mdf");          try         {             string sqlconnectionstring = "data source=.\\sqlexpress;attachdbfilename=" + getparentpath() + "\\database\\test_db.mdf" + ";integrated security=sspi;user instance=true";              fileinfo file = new fileinfo(getparentpath() + "\\test_db.mdf.sql");              string script = file.opentext().readtoend();              sqlconnection conn = new sqlconnection(sqlconnectionstring);              server server = new server(new serverconnection(conn));              server.connectioncontext.executenonquery(script);             file.opentext().close();             messagebox.show("database created successfully", "create database", messageboxbuttons.ok, messageboxicon.information);          }         catch (system.exception ex)         {             messagebox.show(ex.tostring(), "create database", messageboxbuttons.ok, messageboxicon.information);         }                 {             //if (myconn.state == connectionstate.open)             //{             //    myconn.close();             //}         }      }     private string getparentpath()     {         string dbpath = system.appdomain.currentdomain.basedirectory;         int posn;         (int counter = 0; counter != 2; counter++)         {             posn = dbpath.lastindexof("\\");             dbpath = dbpath.substring(0, posn);         }         return dbpath;     } } 

}

creating file mdf extension doesn't mean valid db file. , giving error because isn't valid db file.

if want create database, use code. ofcourse change parameters wish.

string str;     sqlconnection myconn = new sqlconnection ("server=localhost;integrated security=sspi;database=master");      str = "create database mydatabase on primary " +         "(name = mydatabase_data, " +         "filename = 'c:\\mydatabasedata.mdf', " +         "size = 2mb, maxsize = 10mb, filegrowth = 10%) " +         "log on (name = mydatabase_log, " +         "filename = 'c:\\mydatabaselog.ldf', " +         "size = 1mb, " +         "maxsize = 5mb, " +         "filegrowth = 10%)";      sqlcommand mycommand = new sqlcommand(str, myconn);     try      {         myconn.open();     mycommand.executenonquery();     messagebox.show("database created successfully", "myprogram", messageboxbuttons.ok, messageboxicon.information);     }     catch (system.exception ex)     {     messagebox.show(ex.tostring(), "myprogram", messageboxbuttons.ok, messageboxicon.information);     }         {     if (myconn.state == connectionstate.open)     {         myconn.close();     }     } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -