sql 脚本执行类
sql 脚本执行类
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Data.SqlClient;5
using System.Diagnostics;6

7
namespace SqlScript8


{9
public class SqlExcuteScript10

{11

12
private SqlServer server;13

14
public SqlServer Server15

{16
get17

{18
if (server == null)19

{20
server = new SqlServer();21
}22
return server;23
}24

set
{ server = value; }25
}26

27
public void ExcuteScript(string dataBase, string sqlScriptStr)28

{29
try30

{31
Server.Database = dataBase;32
SqlCommand cmd = new SqlCommand(sqlScriptStr, Server.Connection);33
cmd.Connection.Open();34
cmd.ExecuteNonQuery();35
}36
catch (Exception ex)37

{38
Debug.Write(ex.ToString());39
}40
finally41

{42
cmd.Connection.Close();43
}44
}45
}46

47
public class SqlServer48

{49

50
public SqlServer()51

{52
serverIp = "192.168.0.199";53
database = "master";54
uid = "sa";55
pwd = "sa";56
timeout = 3600;57
}58

59
public SqlServer(string connectionString)60

{61
serverIp = "192.168.0.199";62
database = "master";63
uid = "sa";64
pwd = "sa";65
timeout = 3600;66
string[] items = connectionString.Split(';');67
string[][] item = new string[int.Parse(items.Length.ToString())][];68
for (int i = 0; i < items.Length; i++)69

{70
item[i] = new string[2];71
item[i] = items[0].Split('=');72
}73
if (item.Length < 4)74
throw new Exception("数据库连接字符串出错。");75
serverIp = item[0][1].ToString();76
database = item[1][1].ToString();77
uid = item[2][1].ToString();78
pwd = item[3][1].ToString();79
if (item.Length > 5)80
timeout = int.Parse(item[4][1]);81

82
}83
public SqlConnection Connection84

{85
get86

{87
return new SqlConnection(this.ToString());88
}89
}90

91
private string serverIp;92

93
public string ServerIp94

{95

get
{ return serverIp; }96

set
{ serverIp = value; }97
}98

99
private string database;100

101
public string Database102

{103

get
{ return database; }104

set
{ database = value; }105
}106

107
private string uid;108

109
public string Uid110

{111

get
{ return uid; }112

set
{ uid = value; }113
}114

115
private string pwd;116

117
public string Pwd118

{119

get
{ return pwd; }120

set
{ pwd = value; }121
}122

123
private int timeout;124

125
public int TimeOut126

{127

get
{ return timeout; }128

set
{ timeout = value; }129
}130

131
public override string ToString()132

{133
string connStr = String.Format("server={0};database={1};uid={2};pwd={3};timeout={4};",134
serverIp, database, uid, pwd, timeout);135
return connStr;136
}137

138
}139
}140

浙公网安备 33010602011771号