欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#创建数据库及导入sql脚本的方法

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:C#通过正则表达式实现提取网页中的图片

栏    目:C#教程

下一篇:详解C#正则表达式Regex常用匹配

本文标题:C#创建数据库及导入sql脚本的方法

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6787.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有