服务器之家

服务器之家 > 正文

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

时间:2021-11-05 11:01     来源/作者:思齐_

本文实例讲述了C#创建数据库及导入sql脚本的方法。分享给大家供大家参考,具体如下:

C#创建数据库:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// <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脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/// <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#程序设计有所帮助。

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部