C#創(chuàng)建數(shù)據(jù)庫及附加數(shù)據(jù)庫的操作方法
本文實(shí)例講述了C#創(chuàng)建數(shù)據(jù)庫及附加數(shù)據(jù)庫的操作方法。分享給大家供大家參考,具體如下:
/// <summary>
/// 附加數(shù)據(jù)庫方法
/// </summary>
/// <param name="strSql">連接數(shù)據(jù)庫字符串,連接master系統(tǒng)數(shù)據(jù)庫</param>
/// <param name="DataName">數(shù)據(jù)庫名字</param>
/// <param name="strMdf">數(shù)據(jù)庫文件MDF的路徑</param>
/// <param name="strLdf">數(shù)據(jù)庫文件LDF的路徑</param>
/// <param name="path">安裝目錄</param>
private void CreateDataBase( string strSql, string DataName, string strMdf, string strLdf, string path)
{
SqlConnection myConn = new SqlConnection(strSql);
String str = null ;
try
{
str = " EXEC sp_attach_db @dbname='"+DataName+"',@filename1='"+strMdf+"',@filename2='"+strLdf+"'";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("數(shù)據(jù)庫安裝成功!點(diǎn)擊確定繼續(xù)");//需Using System.Windows.Forms
}
catch(Exception e)
{
MessageBox.Show("數(shù)據(jù)庫安裝失敗!" + e.Message+"\n\n"+"您可以手動附加數(shù)據(jù)");
System.Diagnostics.Process.Start(path);//打開安裝目錄
}
finally
{
myConn.Close();
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
string server = this.Context.Parameters["server"];//服務(wù)器名稱
string uid = this.Context.Parameters["user"];//SQlServer用戶名
string pwd = this.Context.Parameters["pwd"];//密碼
string path = this.Context.Parameters["targetdir"];//安裝目錄
string strSql = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=master";//連接數(shù)據(jù)庫字符串
string DataName = "JXC";//數(shù)據(jù)庫名
string strMdf = path + @"JXC.mdf";//MDF文件路徑,這里需注意文件名要與剛添加的數(shù)據(jù)庫文件名一樣!
string strLdf = path + @"jxc_log.ldf";//LDF文件路徑
base.Install(stateSaver);
this.CreateDataBase(strSql, DataName, strMdf, strLdf, path);//開始創(chuàng)建數(shù)據(jù)庫
}
/// <summary>
/// 測試連接
/// </summary>
/// <param name="serverName"></param>
/// <param name="dbName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
private SqlConnection TestConnection(string serverName, string dbName, string userName, string password)
{
string connectionString = GetConnectionString(serverName, dbName, userName, password);
SqlConnection connection = new SqlConnection(connectionString);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
return connection;
}
catch
{
CloseConnection(connection);
throw new InstallException("安裝失敗!\n數(shù)據(jù)庫配置有誤,請正確配置信息!");
}
}
/// <summary>
/// 得到連接字符串
/// </summary>
/// <param name="serverName"></param>
/// <param name="dbName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
private string GetConnectionString(string serverName, string dbName, string userName, string password)
{
string connectionString = "Data Source={0};Initial Catalog={1};User ID={2};Password={3}";
connectionString = string.Format(connectionString, serverName, dbName, userName, password);
return connectionString;
}
/// <summary>
/// 創(chuàng)建數(shù)據(jù)庫
/// </summary>
/// <param name="serverName"></param>
/// <param name="dbName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="connection"></param>
/// <param name="stateSaver"></param>
public int CreateDataBase(SqlConnection connection)
{
int result = -1;
connection.ChangeDatabase("master");
string createDBSql = @" if Exists(select 1 from sysdatabases where [name]=N'{0}')
begin
drop database {0}
end
GO
CREATE DATABASE {0} ";
createDBSql = string.Format(createDBSql, _dbName);
//因?yàn)橛蠫o在SQLCommand中不認(rèn)識,所以以Go為分隔符取sql語句
char[] split = new char[] { 'G', 'O' };
string[] sqlList = createDBSql.Split(split);
SqlCommand command = null;
try
{
command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
foreach (string sqlItem in sqlList)
{
if (sqlItem.Length > 2)
{
command.CommandText = sqlItem;
result = command.ExecuteNonQuery();
}
}
return result;
}
catch
{
CloseConnection(connection);
command.Dispose();
throw new InstallException("安裝失敗!\n數(shù)據(jù)庫配置不正確!");
}
}
/// <summary>
/// 分隔SQL語句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private string[] splitSql(string sql)
{
Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string[] sqlList = regex.Split(sql.ToUpper());
return sqlList;
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
- C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
- C#自動創(chuàng)建數(shù)據(jù)庫實(shí)現(xiàn)代碼
- C#連接操作 MySQL 數(shù)據(jù)庫實(shí)例(使用官方驅(qū)動)
- c#連接數(shù)據(jù)庫及sql2005遠(yuǎn)程連接的方法
- C#連接mysql數(shù)據(jù)庫完整實(shí)例
- C#連接數(shù)據(jù)庫和更新數(shù)據(jù)庫的方法
- C#查詢SqlServer數(shù)據(jù)庫并返回單個值的方法
- c#數(shù)據(jù)綁定之向查詢中添加參數(shù)(.Net連接外部數(shù)據(jù)庫)
- 利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫查詢
相關(guān)文章
C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法,針對decimal變量保留2位有效小數(shù)有多種方法,可以使用Math.Round方法以及ToString先轉(zhuǎn)換為字符串等操作來實(shí)現(xiàn)。具體實(shí)現(xiàn)方法感興趣的朋友跟隨小編一起看看吧2019-10-10
C#實(shí)現(xiàn)讀寫ini文件類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)讀寫ini文件類,實(shí)例分析了C#實(shí)現(xiàn)針對ini文件的讀、寫、刪除等操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
這篇文章主要介紹了C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet的方法,非常簡單實(shí)用,從本人項(xiàng)目中提取出來的,推薦給大家,希望對大家學(xué)習(xí)C#能夠有所幫助。2015-03-03

