C#创建数据库
首先在头部加上引用:
using System.Data;
using System.Data.SqlClient;
然后定义下列变量:
private string serverIP= "10.30.11.21 ";
private string ConnectionString = "Data Source=DMFANGLT;Integrated Security=SSPI;Initial Catalog=;user id=sa;password=best ";
private SqlConnection conn = null;
private SqlCommand cmd = null;
private string sql = null;
在FROM1中加入下列语句:
public Form1()
{
InitializeComponent();
conn = new SqlConnection(ConnectionString);
}
最后在事件中执行下列代码:
if( conn.State != ConnectionState.Open)
conn.Open();
string sql = "CREATE DATABASE mydb ON PRIMARY (name=test_data, filename = 'C:\\mysql\\mydb_data.mdf ', size=3, "
+ "maxsize=5, filegrowth=10%)log on (name=mydbb_log,filename= 'C:\\mysql\\mydb_log.ldf ',size=3,maxsize=20,filegrowth=1) ";
cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException ae)
{
MessageBox.Show(ae.Message.ToString());
}
出现问题目为:Data Source为本机名时可以,但为本机IP或为其它机器IP时出错,错误为:
未处理的“
System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
请问高手如何解决.
------解决方案--------------------Integrated Security=SSPI
改为
Integrated Security=false
------解决方案--------------------Integrated Security=SSPI
改为
Integrated Security=false