-
C#教程之C#自动创建数据库实现代码
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;
public partial class slu1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (execfile())
{
Response.Write("Success");
}
}
}
/// <summary>
/// 创建连接起用进程建立数据库
/// </summary>
/// <returns></returns>
private bool execfile()
{
try
{
string connStr = "data source=127.0.0.1;user id=sa;password=sa;persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE" + " SqlTest");//调用ExecuteNonQuery()来创建数据库
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();//创建一个进程
sqlProcess.StartInfo.FileName = "osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册)
//string str = @"C:\Program Files\Microsoft SQL Server\MSSQL\Data";
sqlProcess.StartInfo.Arguments = " -U sa -P sa -d SqlTest -i C:\\Program Files\\Microsoft SQL Server\\MSSQL\\Data";//获取启动程序时的参数
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建数据库,调用ExecuteNonQuery()执行
/// </summary>
/// <param name="conn"></param>
/// <param name="DatabaseName"></param>
/// <param name="Sql"></param>
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
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;
public partial class slu1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (execfile())
{
Response.Write("Success");
}
}
}
/// <summary>
/// 创建连接起用进程建立数据库
/// </summary>
/// <returns></returns>
private bool execfile()
{
try
{
string connStr = "data source=127.0.0.1;user id=sa;password=sa;persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE" + " SqlTest");//调用ExecuteNonQuery()来创建数据库
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();//创建一个进程
sqlProcess.StartInfo.FileName = "osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册)
//string str = @"C:\Program Files\Microsoft SQL Server\MSSQL\Data";
sqlProcess.StartInfo.Arguments = " -U sa -P sa -d SqlTest -i C:\\Program Files\\Microsoft SQL Server\\MSSQL\\Data";//获取启动程序时的参数
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建数据库,调用ExecuteNonQuery()执行
/// </summary>
/// <param name="conn"></param>
/// <param name="DatabaseName"></param>
/// <param name="Sql"></param>
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式