[转帖]C#执行SQL脚本,读取XML文件

需要添加如下引用:

using System.IO;
using System.Data.SqlClient;
using System.Collections;
using System.Xml;

GoSql类如下:

    class GoSql
    {
        private static string ConStr = "";
        private static string ConString//这里读取XML配置文件可以借鉴
        {
            get
            {
                if (ConStr == "")
                {
                    try
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load("SqlScripts\\ServerConfig.xml");//这里是所执行的SQL服务器配置XML路径
                        string userid = doc.SelectSingleNode("ServerConfig/UserId").InnerText;
                        string password = doc.SelectSingleNode("ServerConfig/PassWord").InnerText;
                        string servername = doc.SelectSingleNode("ServerConfig/ServerName").InnerText;
                        string database = doc.SelectSingleNode("ServerConfig/DataBase").InnerText;
                        ConStr = "server = " + servername + ";uid = "
                         + userid + ";pwd = " + password + ";database = " + database;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                return ConStr;
            }
        }
        private static SqlConnection Con;
        public static SqlConnection MyConnection
        {
            get
            {
                if (Con == null)
                {
                    Con = new SqlConnection(ConString);
                }
                return Con;
            }
        }
        public static bool ExecuteSqlFile(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(alSql);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static void ExecuteCommand(ArrayList varSqlList)
        {
            MyConnection.Open();
            SqlTransaction varTrans = MyConnection.BeginTransaction();
            SqlCommand command = new SqlCommand();
            command.Connection = MyConnection;
            command.Transaction = varTrans;
            try
            {
                foreach (string varcommandText in varSqlList)
                {
                    command.CommandText = varcommandText;
                    command.ExecuteNonQuery();
                }
                varTrans.Commit();
            }
            catch (Exception ex)
            {
                varTrans.Rollback();
                throw ex;
            }
            finally
            {
                MyConnection.Close();
            }
        }
    }

      配置文件ServerConfig.xml内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<ServerConfig>
 <ServerName>192.168.0.68</ServerName>
 <DataBase>lanxi_stgylmis</DataBase>
 <UserId>sa</UserId>
 <PassWord>sa</PassWord>
</ServerConfig>

     调用方法如下:

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {//告诉文件路径即可
                if (GoSql.ExecuteSqlFile(Application.StartupPath.ToString() + "\\SqlScripts\\080417.sql"))
                {
                    MessageBox.Show("SQL语句被执行!");
                }
                else
                {
                    MessageBox.Show("操作失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }


转自:http://www.smartgz.com/blog/Article/1114.asp

posted on 2008-04-23 15:48  Cxywzx  阅读(1960)  评论(0编辑  收藏  举报

导航