sql 脚本执行类


ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.Data.SqlClient;
  5None.gifusing System.Diagnostics;
  6None.gif
  7None.gifnamespace SqlScript
  8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  9InBlock.gif    public class SqlExcuteScript
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 11InBlock.gif
 12InBlock.gif        private SqlServer server;
 13InBlock.gif
 14InBlock.gif        public SqlServer Server
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 16InBlock.gif            get
 17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 18InBlock.gif                if (server == null)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 20InBlock.gif                    server = new SqlServer();
 21ExpandedSubBlockEnd.gif                }

 22InBlock.gif                return server;
 23ExpandedSubBlockEnd.gif            }

 24ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ server = value; }
 25ExpandedSubBlockEnd.gif        }

 26InBlock.gif
 27InBlock.gif        public void ExcuteScript(string dataBase, string sqlScriptStr)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            try
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                Server.Database = dataBase;
 32InBlock.gif                SqlCommand cmd = new SqlCommand(sqlScriptStr, Server.Connection);
 33InBlock.gif                cmd.Connection.Open();
 34InBlock.gif                cmd.ExecuteNonQuery();
 35ExpandedSubBlockEnd.gif            }

 36InBlock.gif            catch (Exception ex)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                Debug.Write(ex.ToString());
 39ExpandedSubBlockEnd.gif            }

 40InBlock.gif            finally
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 42InBlock.gif                cmd.Connection.Close();
 43ExpandedSubBlockEnd.gif            }

 44ExpandedSubBlockEnd.gif        }

 45ExpandedSubBlockEnd.gif    }

 46InBlock.gif
 47InBlock.gif    public class SqlServer
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 49InBlock.gif
 50InBlock.gif        public SqlServer()
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            serverIp = "192.168.0.199";
 53InBlock.gif            database = "master";
 54InBlock.gif            uid = "sa";
 55InBlock.gif            pwd = "sa";
 56InBlock.gif            timeout = 3600;
 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif
 59InBlock.gif        public SqlServer(string connectionString)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            serverIp = "192.168.0.199";
 62InBlock.gif            database = "master";
 63InBlock.gif            uid = "sa";
 64InBlock.gif            pwd = "sa";
 65InBlock.gif            timeout = 3600;
 66InBlock.gif            string[] items = connectionString.Split(';');
 67InBlock.gif            string[][] item = new string[int.Parse(items.Length.ToString())][];
 68InBlock.gif            for (int i = 0; i < items.Length; i++)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                item[i] = new string[2];
 71InBlock.gif                item[i] = items[0].Split('=');
 72ExpandedSubBlockEnd.gif            }

 73InBlock.gif            if (item.Length < 4)
 74InBlock.gif                throw new Exception("数据库连接字符串出错。");
 75InBlock.gif            serverIp = item[0][1].ToString();
 76InBlock.gif            database = item[1][1].ToString();
 77InBlock.gif            uid = item[2][1].ToString();
 78InBlock.gif            pwd = item[3][1].ToString();
 79InBlock.gif            if (item.Length > 5)
 80InBlock.gif                timeout = int.Parse(item[4][1]);
 81InBlock.gif
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif        public SqlConnection Connection
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            get
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                return new SqlConnection(this.ToString());
 88ExpandedSubBlockEnd.gif            }

 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91InBlock.gif        private string serverIp;
 92InBlock.gif
 93InBlock.gif        public string ServerIp
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn serverIp; }
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ serverIp = value; }
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        private string database;
100InBlock.gif
101InBlock.gif        public string Database
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn database; }
104ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ database = value; }
105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107InBlock.gif        private string uid;
108InBlock.gif
109InBlock.gif        public string Uid
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn uid; }
112ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ uid = value; }
113ExpandedSubBlockEnd.gif        }

114InBlock.gif
115InBlock.gif        private string pwd;
116InBlock.gif
117InBlock.gif        public string Pwd
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn pwd; }
120ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ pwd = value; }
121ExpandedSubBlockEnd.gif        }

122InBlock.gif
123InBlock.gif        private int timeout;
124InBlock.gif
125InBlock.gif        public int TimeOut
126ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
127ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn timeout; }
128ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ timeout = value; }
129ExpandedSubBlockEnd.gif        }

130InBlock.gif
131InBlock.gif        public override string ToString()
132ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
133InBlock.gif            string connStr = String.Format("server={0};database={1};uid={2};pwd={3};timeout={4};",
134InBlock.gif                serverIp, database, uid, pwd, timeout);
135InBlock.gif            return connStr;
136ExpandedSubBlockEnd.gif        }

137InBlock.gif
138ExpandedSubBlockEnd.gif    }

139ExpandedBlockEnd.gif}

140None.gif

转载于:https://www.cnblogs.com/wubiyu/archive/2007/07/05/807235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值