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足量 U盘 | |
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;
}
}
}