程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#創立數據庫及導入sql劇本的辦法

C#創立數據庫及導入sql劇本的辦法

編輯:C#入門知識

C#創立數據庫及導入sql劇本的辦法。本站提示廣大學習愛好者:(C#創立數據庫及導入sql劇本的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#創立數據庫及導入sql劇本的辦法正文


本文實例講述了C#創立數據庫及導入sql劇本的辦法。分享給年夜家供年夜家參考,詳細以下:

C#創立數據庫:

/// <summary>
/// 創立數據庫
/// </summary>
/// <param name="connStr">銜接字符串</param>
/// <param name="_strDBName">數據庫稱號</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#導入sql劇本:

/// <summary>
/// 導入sql劇本
/// </summary>
/// <param name="sqlConnString">銜接數據庫字符串</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();
  }
 }
}

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved