• [转帖]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
  • 相关阅读:
    day09页面的声明周期函数
    day8小程序的运行环境与基本架构
    day09小程序复习
    day08前后端交互
    day07获取图片
    day07获取用户地址信息
    MySQL 主从同步延迟的原因及解决办法
    升级Oracle 19c经验: TTS 在使用datapump导matadata时EXCLUDE=STATISTICS 不启作用
    12c,19c自动kill长时间未活动会话特性
    SuSE11单实例二进制安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/saptechnique/p/1782234.html
Copyright © 2020-2023  润新知