zoukankan      html  css  js  c++  java
  • C# 调用 *.sql 文件

    源:http://zhidao.baidu.com/question/5367984.html?si=2

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

    namespace ExecuteSqlFile
    {
    /// <summary>
    /// DBAccess 的摘要说明。
    /// </summary>
    public class DBAccess
    {
    public DBAccess()
    {
    }

    #region 属性

    private static string ConStr = "";

    private static string ConString
    {
    get
    {
    if(ConStr == "")
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load("ServerConfig.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;
    }
    }

    #endregion


    /// <summary>
    /// 执行Sql文件
    /// </summary>
    /// <param name="varFileName"></param>
    /// <returns></returns>
    public static bool ExecuteSqlFile(string varFileName)
    {
    if(!File.Exists(varFileName))
    {
    return false;
    }

    StreamReader sr = File.OpenText(varFileName);

    ArrayList alSql = new ArrayList();

    string commandText = "";

    string varLine = "";

    while(sr.Peek() > -1)
    {
    varLine = sr.ReadLine();
    if(varLine == "")
    {
    continue;
    }
    if(varLine != "GO")
    {
    commandText += varLine;
    commandText += "\r\n";
    }
    else
    {
    alSql.Add(commandText);
    commandText = "";
    }
    }

    sr.Close();

    try
    {
    ExecuteCommand(alSql);
    }
    catch
    {
    return false;
    }

    return true;
    }


    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();
    }
    }
    }
    }



    配置文件如下:
    <?xml version="1.0" encoding="utf-8" ?>
    <ServerConfig>
    <ServerName>localhost</ServerName>
    <DataBase>tttttt</DataBase>
    <UserId>sa</UserId>
    <PassWord>sa</PassWord>
    </ServerConfig>
    保存为ServerConfig.xml
    放到Bin/Debug/里面




    调用如下:
    DBAccess.ExecuteSqlFile(@"E:\My Document\MySql\test1.sql");
  • 相关阅读:
    Mysql 时间操作
    curl 学习
    CURL详解
    mysql 获取当前时间戳
    php开启openssl的方法
    0,null,empty,空,false,isset
    ecshop微信扫描支付开发
    seaJs的简单应用
    js运动框架之掉落的扑克牌(重心、弹起效果)
    js运动框架完成块的宽高透明度及颜色的渐变
  • 原文地址:https://www.cnblogs.com/yiki/p/676945.html
Copyright © 2011-2022 走看看