欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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#變量命名規(guī)則小結(jié)

    C#變量命名規(guī)則小結(jié)

    本文主要介紹了C#變量命名規(guī)則小結(jié),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    這篇文章介紹了C#實(shí)現(xiàn)INotifyPropertyChanged接口的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#如何連接使用Zookeeper

    C#如何連接使用Zookeeper

    Zookeeper作為分布式的服務(wù)框架,雖然是java寫的,但是強(qiáng)大的C#也可以連接使用。而現(xiàn)在主要有兩個(gè)插件可供使用,分別是ZooKeeperNetEx和Zookeeper.Net,個(gè)人推薦使用ZooKeeperNetEx做開發(fā),本文也已介紹ZooKeeperNetEx為主
    2021-06-06
  • Unity接入百度AI實(shí)現(xiàn)貨幣識(shí)別

    Unity接入百度AI實(shí)現(xiàn)貨幣識(shí)別

    本文主要介紹了在Unity中接入百度AI,從而實(shí)現(xiàn)貨幣識(shí)別,可以返回貨幣的名稱、代碼、面值、年份信息等,感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-01-01
  • C#中增強(qiáng)類功能的幾種方式詳解

    C#中增強(qiáng)類功能的幾種方式詳解

    這篇文章主要給大家介紹了關(guān)于C#中增強(qiáng)類功能的幾種方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • C# WinForm編程獲取文件物理路徑的方法

    C# WinForm編程獲取文件物理路徑的方法

    這篇文章主要介紹了C# inForm編程獲取文件物理路徑的方法,獲取的物理路徑是軟件即軟件安裝所在目錄,需要的朋友可以參考下
    2014-08-08
  • C# linq查詢之動(dòng)態(tài)OrderBy用法實(shí)例

    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)用攝像頭的示例詳解

    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#構(gòu)造ColorComboBox(顏色下拉框)

    c#構(gòu)造ColorComboBox(顏色下拉框)

    這篇文章主要介紹了c#構(gòu)造ColorComboBox的代碼分享,大家參考使用吧
    2013-12-12
  • C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫的方法

    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

最新評(píng)論