国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
c#安裝數(shù)據(jù)庫并自動修改Web.config類

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Configuration.Install;
using System.Collections;
using System.Management;
namespace DLL
{
   public class CreatSQL
    {
        //成員定義

        private System.Data.SqlClient.SqlConnection sqlConn=new SqlConnection();
        private System.Data.SqlClient.SqlCommand Command=new SqlCommand();
        private string _ServerName = ".";
        private string _dbname = "";
        private string _userid = "sa";
        private string _pwd = "sa";
        private string _sqlfile = "dbsql.sql";

//屬性定義
        //服務器名稱
        public string ServerName
        {
            set { _ServerName = value; }
            get { return _ServerName; }
        }
        //數(shù)據(jù)庫名稱
        public string dbname
        {
            set { _dbname = value; }
            get { return _dbname; }
        }
        //用戶名
        public string userid
        {
            set { _userid = value; }
            get { return _userid; }
        }
        //密碼
        public string pwd
        {
            set { _pwd = value; }
            get { return _pwd; }
        }
        //sql腳本文件名
        public string sqlfile
        {
            set { _sqlfile = value; }
            get { return _sqlfile; }
        }

//方法定義
        //連接數(shù)據(jù)庫服務器到方法:
        #region ConnectDatabase 連接數(shù)據(jù)庫
        private bool Conn()
        {
            sqlConn.ConnectionString = "Data Source=" + this.ServerName + ";Initial Catalog=master;User ID=" + this.userid + ";Password=" +this.pwd;
                try
                {
                    sqlConn.Open();
                    if (sqlConn.State == ConnectionState.Open)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                   
                }
                catch
                {
                    return false;
                }
           
        
        }
        #endregion
      
        //讀取SQL文件的方法:
        #region GetSql 從文件中讀取SQL,在讀取包含SQL腳本的文件時需要用到
        public  bool ExecuteSqlFile(string FileName)
        {
         
            if (!File.Exists(FileName))
            {
                return false;
            }

            StreamReader sr = File.OpenText(FileName);
            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;
        }
        #endregion
        //執(zhí)行SQL語句的方法:
        #region ExecuteSql 執(zhí)行SQL語句,參考自MSDN
        public void ExecuteSql(string DataBaseName, string sqlstring)
        {

            if (Conn())
            {
                Command = new System.Data.SqlClient.SqlCommand(sqlstring, sqlConn);
                try
                {
                    //Command.Connection.ChangeDatabase(DataBaseName);
                    Command.ExecuteNonQuery();
                }
                finally
                {
                    Command.Connection.Close();
                }
            }
           
        }
        #endregion
        //創(chuàng)建數(shù)據(jù)庫及數(shù)據(jù)庫表:
        #region CreateDBAndTable 創(chuàng)建數(shù)據(jù)庫及數(shù)據(jù)庫表,參考自MSDN

        public bool CreateDBAndTable()
        {
            bool Restult = false;
            try
            {
                ExecuteSql("master", "USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + this.dbname + "') DROP DATABASE " + this.dbname);
                ExecuteSql("master", "CREATE DATABASE " + this.dbname);
                ExecuteSqlFile(this.sqlfile);
                Restult = true;
            }
            catch
            {
            }
            return Restult;
        }
        #endregion
        #region WriteWebConfig 修改web.config的連接數(shù)據(jù)庫的字符串
        public bool WriteWebConfig(string config, string ConnectionString)
        {
            System.IO.FileInfo FileInfo = new System.IO.FileInfo(config);
            if (!FileInfo.Exists)
            {
                throw new InstallException("Missing config file :" +config);
            }
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(FileInfo.FullName);
            bool FoundIt = false;
            foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"])
            {
                if (Node.Name == "add")
                {
                    if (Node.Attributes.GetNamedItem("key").Value == ConnectionString)
                    {
                        Node.Attributes.GetNamedItem("value").Value = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096", this.ServerName,this.dbname,this.userid,this.pwd);
                        FoundIt = true;
                    }
                }
            }
            if (!FoundIt)
            {
                throw new InstallException("Error when writing the config file: web.config");
            }
            xmlDocument.Save(FileInfo.FullName);
            return FoundIt;
        }
        #endregion
        #region 執(zhí)行SQL腳本的每一行命令
        private  void ExecuteCommand(ArrayList varSqlList)
         
            try
            {
               if (Conn())
                               
                    foreach (string commandText in varSqlList)
                    {
                        Command = new System.Data.SqlClient.SqlCommand(commandText, sqlConn);
                        Command.ExecuteNonQuery();
                    }
                          
            }
            catch (Exception ex)
                     
                throw ex;
            }
        }
        #endregion
    }

}

調(diào)用方法示例:

 DLL.CreatSQL cb = new DLL.CreatSQL();
   string dir = @"C:\Documents and Settings\nature\My Documents\Visual Studio 2008\Projects\huatu\Web\";
            cb.dbname = "test";
            cb.sqlfile = dir+"dbsql2.sql";
            cb.userid = "sa";
            cb.pwd = "sa";
            cb.ServerName = ".";
            cb.CreateDBAndTable();
            cb.WriteWebConfig(dir + "Web.config", "ConnectionString");

dbsql2.sql,文件示例:

use [test]

GO

CREATE TABLE [dbo].[TT] (
 [NAME] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
 [DEPARTMENT] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
 [wage] [decimal](9, 2) NULL
) ON [PRIMARY]
GO

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用 GUID 值來作為數(shù)據(jù)庫行標識
.NET三層經(jīng)典架構(gòu)PetShop3.0分析連載一-.NET教程,.NET Framewo...
XML配置文件代替INI(VB.NET)
INI文件格式及其操作代碼
asp制作用戶登陸界面--龍二
ASP與SQL Server存儲過程
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服