using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using Dvbbs.Utils;
using Dvbbs.bbs.Entity;
using Dvbbs.bbs.Handlers;
using System.Xml;
public partial class InstallDB_DBinstall : System.Web.UI.Page
{
protected string server = string.Empty;
protected string dbName = string.Empty;
protected string uid = string.Empty;
protected string pwd = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
server = this.ttbServer.Text;
dbName = this.ttbDBName.Text;
uid = this.ttbUser.Text;
pwd = this.ttbPwd.Text;
try
{
string connstr = String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", server, uid, pwd);
//'根據輸入的資料庫名稱建立資料庫
ExecuteSql(connstr, "master", "create database " + dbName);
Response.Write("<script language='javascript'>alert('提交成功');</script>");
//調用osql執行指令碼
// Process sqlprocess = new Process();
// sqlprocess.StartInfo.FileName = "osql.exe";//啟動應用程式osql.exe
// sqlprocess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", uid, pwd, dbName, "/InstallDB/");
// sqlprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// sqlprocess.Start();//執行應用程式
// sqlprocess.WaitForExit(); //等待執行
// sqlprocess.Close();//釋放與此關聯的所有的資源
// //'刪除指令檔
// FileInfo sqlfileinfo = new FileInfo(String.Format("{0}db.sql", "/InstallDB/"));//sql檔案存放的位置
// if (sqlfileinfo.Exists)
// {
// sqlfileinfo.Delete();
// }
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
string filePath = Server.MapPath("/InstallDB/db1.sql");
ExecuteSqlFile(filePath);
EditConfig();
}
///////////////////////執行sql語句////////////////////////////
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{//建立空庫
SqlConnection mySqlConnection = new SqlConnection(conn);
SqlCommand Command = new SqlCommand(Sql, mySqlConnection);
mySqlConnection.Open();
mySqlConnection.ChangeDatabase(DatabaseName);// 改變已串連的資料庫連接
try
{
Command.ExecuteNonQuery();//執行sql語句
}
finally
{
mySqlConnection.Close();
}
}
////建立 sql server 資料庫
//private bool CreateSqlServerDataBase(string mTablePrefix)
//{
// if (conn.Provider == DbProviderEnum.Access)
// {
// return false;
// }
// // 讀取 mssql.sql 中 sql 語句
// string sqlfile = Fetch.MapPath("install\\mssql.sql");
// string stmt = "";
// using (StreamReader reader = new StreamReader(sqlfile, System.Text.Encoding.GetEncoding("GB2312")))
// {
// string[] arr = reader.ReadToEnd().Split('~');
// foreach (string sql in arr)
// {
// stmt = sql.Trim();
// if (0 == stmt.Length)
// {
// continue;
// }
// conn.Execute(stmt);
// }
// return true;
// }
// return true;
//}
///////////////////////執行sql檔案//////////////////////
public 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 void ExecuteCommand(ArrayList varSqlList)
{
try
{
SqlConnection MyConnection = new SqlConnection(ConString());
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();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
}
private string ConString()
{
string connString="server="+server+";database="+dbName+";uid="+uid+";pwd="+pwd+";";
return connString;
}
/////////////////////////////////////////修改web.config////////////////////////////
//' ---------------------將連接字串寫入Web.config-----------------------------------
private void EditConfig()
{
try
{
string ConfigFile = Server.MapPath("http://www.cnblogs.com/yuanlei347/admin/file://web.config/");
FileInfo fileinfo = new FileInfo(ConfigFile);
if (!fileinfo.Exists)
{
//throw new InstallException("沒有找到設定檔");
}
//'執行個體化xml文檔
XmlDocument xmldocument = new XmlDocument();
xmldocument.Load(fileinfo.FullName);
//'尋找到appsettings中的節點
//XmlNode node=new XmlNode();
Boolean FoundIt = false;
foreach (XmlNode node in xmldocument.SelectSingleNode("appSettings").ChildNodes)
{
if (node.Name == "add")
{
if (node.Attributes.GetNamedItem("key").Value == "test_sql")
{
//'寫入連接字串
node.Attributes.GetNamedItem("value").Value = String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", server,dbName, uid, pwd);
FoundIt = true;
Response.Write("改了");
Response.End();
|
正版KINGSTON 金士頓 4G足量 隨身碟 |
| 62.0元 |
|
}
}
}
if (!FoundIt)
{
Response.Write("沒找到");
//throw new InstallException("web.Config 檔案沒有包含connString連接字串設定");
}
Response.Write("找到");
Response.End();
xmldocument.Save(fileinfo.FullName);
}
catch (Exception ex)
{
throw ex;
}
}
}