C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法
更新時(shí)間:2015年12月10日 14:59:24 作者:思齊_
這篇文章主要介紹了C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法,涉及C#針對(duì)數(shù)據(jù)庫的創(chuàng)建、連接、導(dǎo)入等相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法。分享給大家供大家參考,具體如下:
C#創(chuàng)建數(shù)據(jù)庫:
/// <summary>
/// 創(chuàng)建數(shù)據(jù)庫
/// </summary>
/// <param name="connStr">連接字符串</param>
/// <param name="_strDBName">數(shù)據(jù)庫名稱</param>
/// <returns></returns>
private static bool CreateDatabase(string connStr, string _strDBName)
{
bool bSuccess = false;
try
{
using (SqlConnection conMaster = new SqlConnection(connStr))
{
conMaster.Open();
// Check if the Database has existed first
string strExist = @"select * from dbo.sysdatabases where name='" + _strDBName + @"'";
SqlCommand cmdExist = new SqlCommand(strExist, conMaster);
SqlDataReader readerExist = cmdExist.ExecuteReader();
bool bExist = readerExist.HasRows;
readerExist.Close();
if (bExist)
{
string strDel = @"drop database " + _strDBName;
SqlCommand cmdDel = new SqlCommand(strDel, conMaster);
cmdDel.ExecuteNonQuery();
}
// Create the database now;
string strDatabase = "Create Database [" + _strDBName + "]";
SqlCommand cmdCreate = new SqlCommand(strDatabase, conMaster);
cmdCreate.ExecuteNonQuery();
conMaster.Close();
}
bSuccess = true;
}
catch (Exception e)
{
throw e;
}
return bSuccess;
}
C#導(dǎo)入sql腳本:
/// <summary>
/// 導(dǎo)入sql腳本
/// </summary>
/// <param name="sqlConnString">連接數(shù)據(jù)庫字符串</param>
/// <param name="varFileName">腳本路徑</param>
/// <returns></returns>
private static bool ExecuteSqlFile(string sqlConnString, string varFileName)
{
if (!File.Exists(varFileName))
{
return false;
}
StreamReader rs = new StreamReader(varFileName, System.Text.Encoding.Default);
ArrayList alSql = new ArrayList();
string commandText = "";
string varLine = "";
while (rs.Peek() > -1)
{
varLine = rs.ReadLine();
if (varLine == "")
{
continue;
}
if (varLine != "GO")
{
commandText += varLine;
commandText += "\r\n";
}
else
{
commandText += "";
}
}
alSql.Add(commandText);
rs.Close();
try
{
ExecuteCommand(sqlConnString, alSql);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
private static void ExecuteCommand(string sqlConnString, ArrayList varSqlList)
{
using (SqlConnection conn = new SqlConnection(sqlConnString))
{
conn.Open();
//Don't use Transaction, because some commands cannot execute in one Transaction.
//SqlTransaction varTrans = conn.BeginTransaction();
SqlCommand command = new SqlCommand();
command.Connection = conn;
//command.Transaction = varTrans;
try
{
foreach (string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
//varTrans.Commit();
}
catch (Exception ex)
{
//varTrans.Rollback();
throw ex;
}
finally
{
conn.Close();
}
}
}
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口
這篇文章介紹了C#實(shí)現(xiàn)INotifyPropertyChanged接口的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Unity接入百度AI實(shí)現(xiàn)貨幣識(shí)別
本文主要介紹了在Unity中接入百度AI,從而實(shí)現(xiàn)貨幣識(shí)別,可以返回貨幣的名稱、代碼、面值、年份信息等,感興趣的可以跟隨小編學(xué)習(xí)一下2022-01-01
C# linq查詢之動(dòng)態(tài)OrderBy用法實(shí)例
這篇文章主要介紹了C# linq查詢之動(dòng)態(tài)OrderBy用法,實(shí)例分析了C#采用linq方式查詢時(shí)動(dòng)態(tài)排序的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個(gè)專門為開發(fā)者和研究者基于C#框架設(shè)計(jì)的,這個(gè)框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11
C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫的方法
這篇文章主要介紹了C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫的方法,通過自定義函數(shù)db_connection_test實(shí)現(xiàn)遠(yuǎn)程連接Oracle數(shù)據(jù)庫的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12

