• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#执行sql文件 运行sql文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 private void metroButton2_Click(object sender, EventArgs e)
        {
            string[] contens = new string[0];
            string FileNameSql = "";
            string templateFile = AppDomain.CurrentDomain.BaseDirectory;//程序文件目录
            bool file_b = false;//默认txt文件不存在
            if (Directory.Exists(templateFile))//若文件夹存在
            {
                FileNameSql= templateFile + "\\创建档案条目表.txt";
                file_b = File.Exists(templateFile + "\\创建档案条目表.txt");//文件是否存在
                if (file_b)
                {
                    contens = File.ReadAllLines(templateFile + "\\创建档案条目表.txt");//存在读取这个txt
                }
            }

           //获取到数据库连接信息
            ExecuteCommand(GetSqlFile(FileNameSql, ""),DaoFactory.NewSJDao(Common.g_DataSource));
            MessageBox.Show("创建案卷表和卷内表成功!");
        }

 

        /// <summary>
        /// 读取文件并且修改
        /// </summary>
        /// <param name="varFileName">文件路径</param>
        /// <param name="dbname">数据库表名</param>
        /// <returns></returns>
        public  ArrayList GetSqlFile(string varFileName, string dbname)
        {
            ArrayList alSql = new ArrayList();
            if (!File.Exists(varFileName))
            {
                return alSql;
            }
            StreamReader rs = new StreamReader(varFileName, System.Text.Encoding.Default);//注意编码
            string commandText = "";
            string varLine = "";
            while (rs.Peek() > -1)
            {
                varLine = rs.ReadLine();
                if (varLine == "")
                {
                    continue;
                }
                if (varLine != "GO" && varLine != "go")
                {
                    commandText += varLine;
                    commandText = commandText.Replace("@database_name=N\'dbhr\'", string.Format("@database_name=N\'{0}\'", dbname));
                    commandText += "\r\n";
                }
                else
                {
                    //将@ArchvTable和@ArchvFileTable替换成案卷表名和卷内表名
                    string pcom = "";
                    if(commandText.Contains("@ArchvTable")|| commandText.Contains("@ArchvFileTable"))
                    {
                        pcom= commandText.Replace("@ArchvTable", Common.g_QXPZAJ);
                        pcom = pcom.Replace("@ArchvFileTable", Common.g_QXPZJN);
                    }
                    alSql.Add(pcom);
                    commandText = "";
                }
            }
            rs.Close();
            return alSql;
        }
 
        /// <summary>
        /// 连接数据库表并执行文件中的sql语句
        /// </summary>
        /// <param name="varSqlList"></param>
        /// <param name="connString"></param>
        public static void ExecuteCommand(ArrayList varSqlList, string connString)
        {
            SqlConnection MyConnection = new SqlConnection(connString);
            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();
            }
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
基于FFMpeg的C#录屏全攻略发布时间:2022-07-10
下一篇:
C#WinOSResource发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap