c# for sql script execution -
i have 2 forms, first collecting details datasource, database name, username , password. second form collects sql script file executed , database name connected to..
what want achieve should able execute sql script file in selected database.
the code used in second form this:
private void combobox1_textchanged(object sender, eventargs e) { string sel = combobox1.selecteditem.tostring(); //here "sel" database selected if (sel == "master") { combobox2.items.clear(); //selects default file directoryinfo dinfo = new directoryinfo(@"d:\testpgm"); fileinfo[] files = dinfo.getfiles("master.sql", searchoption.alldirectories); foreach (fileinfo file in files) { combobox2.items.add(file.name); } } else { combobox2.items.clear(); //selects default file directoryinfo dinfo = new directoryinfo(@"d:\testpgm"); fileinfo[] files = dinfo.getfiles("dbscript.sql", searchoption.alldirectories); foreach (fileinfo file in files) { combobox2.items.add(file.name); } } }
and code used in combobox 2 is:
private void combobox2_textchanged(object sender, eventargs e) { string textsel = combobox2.selecteditem.tostring(); //textsel used select sql file selected if (textsel == "master.sql") { richtextbox1.clear(); //read selected sql file , display in richtextbox system.io.streamreader myfile = new system.io.streamreader(@"d:\testpgm\master.sql"); string textentry = myfile.readtoend(); richtextbox1.appendtext(textentry); } else { richtextbox1.clear(); //read selected sql file , display in richtextbox system.io.streamreader myfile = new system.io.streamreader(@"d:\testpgm\dbscript.sql"); string textentry = myfile.readtoend(); richtextbox1.appendtext(textentry); } }
now, want connect database selected in combo box 1 , exceute sql script included in sql file selected in combo box 2, @ click of button.
is possible? how can achieve this?
any comments helpful , appreciated..
by using
using microsoft.sqlserver.management.common; using microsoft.sqlserver.management.smo;
you can below
sqlconnection conn = new sqlconnection(sqlconnectionstring); server server = new server(new serverconnection(conn)); server.connectioncontext.executenonquery(script);
Comments
Post a Comment