C# 呼叫*.sql第一種
using System;
using System.Xml;
using System.Data;
using System.IO;
using System.Collections;
using System.Data.SqlClient;
namespace ExecuteSqlFile
{
///
/// DBAccess 的摘要說明。
///
public class DBAccess
{
public DBAccess()
{
}
#region 屬性
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
///
/// 執行Sql檔案
///
/// 檔案的名稱
///
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 = "";
}
}
sr.Close();
try
{
ExecuteCommand(alSql);
}
catch
{
return false;
}
return true;
}
private static void ExecuteCommand(ArrayList varSqlList)
{
MyConnection.Open();
SqlTransaction varTrans = MyConnection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.Connection = MyConnection;
command.Transaction = varTrans;
try
{
foreach(string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch(Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}
}
}
配置檔案如下:
localhost
tttttt
sa
sa
儲存為ServerConfig.xml
放到Bin/Debug/裡面
using System.Xml;
using System.Data;
using System.IO;
using System.Collections;
using System.Data.SqlClient;
namespace ExecuteSqlFile
{
///
/// DBAccess 的摘要說明。
///
public class DBAccess
{
public DBAccess()
{
}
#region 屬性
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
///
/// 執行Sql檔案
///
/// 檔案的名稱
///
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 = "";
}
}
sr.Close();
try
{
ExecuteCommand(alSql);
}
catch
{
return false;
}
return true;
}
private static void ExecuteCommand(ArrayList varSqlList)
{
MyConnection.Open();
SqlTransaction varTrans = MyConnection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.Connection = MyConnection;
command.Transaction = varTrans;
try
{
foreach(string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch(Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}
}
}
配置檔案如下:
儲存為ServerConfig.xml
放到Bin/Debug/裡面
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526510/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於C#委託三種呼叫的分享C#
- VC++ 呼叫 C#生成DLL的兩種方法C++C#
- 使用C#建立webservice及三種呼叫方式 (轉)C#Web
- C#後臺呼叫前臺javascript的五種方法C#JavaScript
- C#呼叫pydC#
- C#呼叫webserviceC#Web
- c#呼叫webservicesC#Web
- C# 客戶端程式呼叫外部程式的三種實現C#客戶端
- C#呼叫PythonC#Python
- C#呼叫wpsC#
- C#呼叫外部DLLC#
- c#呼叫web serviceC#Web
- c#中呼叫ExcelC#Excel
- c# 反射呼叫方法C#反射
- c#直接呼叫ssis包實現Sql Server的資料匯入功能C#SQLServer
- xLua中C#呼叫LuaC#
- xLua中Lua呼叫C#C#
- C#呼叫C++DLLC#C++
- C#動態呼叫webserviceC#Web
- C#中委託的呼叫C#
- c# 呼叫.bat檔案C#BAT
- C#呼叫匯編dllC#
- C# 生成DLL 並 呼叫C#
- C# netCore Grpc服務 (2)配置 ,proto以及四種呼叫方式C#NetCoreRPC
- 8種最坑的SQL錯誤用法,第一個就很坑?SQL
- C#呼叫 C++的DLLC#C++
- CefSharp ——js呼叫c#方法JSC#
- c# 呼叫微吼直播APIC#API
- C# 呼叫Python程式碼C#Python
- C#中Emgucv呼叫HalconC#
- C#/.net程式呼叫pythonC#Python
- C#動態呼叫WCF介面C#
- C# 呼叫目標異常C#
- 在c#中呼叫confirmC#
- c# 中呼叫COM元件 (轉)C#元件
- C#呼叫js庫的方法C#JS
- PL/SQL呼叫JAVA CLASSSQLJava
- 關於SQL Server中儲存過程在C#中呼叫的簡單示例SQLServer儲存過程C#