How to execute. SQL script (SQL Server) in C #

xiaoxiao2021-03-06  76

I don't like to use osql.exe, isql.exe, although N earlier, use Osql.exe to query the SQL Server server in the LAN. ADO.NET is enough. As long as "Go" is filtered. As follows: use system; using system.data; using system.collections; using system.data.sqlclient;

Namespace ExecuteSqlfile {///

/// DBAccess's summary description. /// public class dbaccess {public dbaccess () {}

#region attribute

Private static string constr = ""

private static string ConString {get {if (ConStr == "") {try {XmlDocument doc = new XmlDocument (); doc.Load ( "ServerConfig.xml"); string userid = doc.SelectSingleNode ( "ServerConfig / UserId") .InnerText; string password = doc.SelectSingleNode ( "ServerConfig / passWord") InnerText;. string servername = doc.SelectSingleNode ( "ServerConfig / ServerName") InnerText;. string database = doc.SelectSingleNode ( "ServerConfig / DataBase") InnerText. Constr = "Server =" servername "; uid =" userid "; pwd =" password "; database =" Database;} catch (exception ex) {throw ex;}}

Return constr;}}

Private static sqlConnection con;

Public static sqlconnection myconnection {get {if (con == null) {con = new sqlConnection (constring);} Return Con;}} #ENDREGION

///

/// Execute SQL file /// /// /// public static bool executesqlfile (String VARFILENAME) {if (! file.exists (varFileName)) {Return False;} streamreader sr = file.opentext (varFileName); arraylist alsql = new arraylist ();

String commandText = ""

String varline = ""

While (sr.peek ()> -1) {varline = sr.readline (); if (varline == ") {Continue;} if (varline! =" go ") {CommandText = varline; commandText = "/ r / n";} else {alsql.add (commandtext); commandtext = "";}}

Try {EXECUTECOMMAND (ALSQL);} catCh {return false;}

Return True;}

Private static void executeCommand (arraylist varsqllist) {myconnection.open (); sqltransaction varratrans = myconnection.begintransaction ();

Sqlcommand command = new sqlcommand (); command.connection = myconnection; command.transaction = vartrion;

转载请注明原文地址:https://www.9cbs.com/read-93461.html

New Post(0)