我想写一上能用页面输入内容来创建数据表的页,发现了一个问题,请大家指教!using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void bottom_Click(object sender, EventArgs e)
{
string ServerName1 = this.servername.Text;
string dataname1 = this.dataname.Text;
string username1 = this.username.Text;
string password1 = this.password.Text;
string TableName1 = this.tablename.Text;
string LieName1 = this.lie.Text;
string strConnection = "user id=" + username1 +";password="+password1 +";" ;
strConnection += "Database="+dataname1+";server="+ServerName1 +";";
strConnection += "Connect Timeout=30";
SqlConnection db1 = new SqlConnection(strConnection);
SqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)");
db1.Open();
Create2.ExecuteNonQuery();
Response.Write("命令执行成功,请检查数据表");
db1.Close();
}
}
运行时提示:ExecuteNonQuery: Connection 属性尚未初始化。
using System.Data;
using System.Configuration;
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.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void bottom_Click(object sender, EventArgs e)
{
string ServerName1 = this.servername.Text;
string dataname1 = this.dataname.Text;
string username1 = this.username.Text;
string password1 = this.password.Text;
string TableName1 = this.tablename.Text;
string LieName1 = this.lie.Text;
string strConnection = "user id=" + username1 +";password="+password1 +";" ;
strConnection += "Database="+dataname1+";server="+ServerName1 +";";
strConnection += "Connect Timeout=30";
SqlConnection db1 = new SqlConnection(strConnection);
SqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)");
db1.Open();
Create2.ExecuteNonQuery();
Response.Write("命令执行成功,请检查数据表");
db1.Close();
}
}
运行时提示:ExecuteNonQuery: Connection 属性尚未初始化。
解决方案 »
- 利用HttpModule重写路径时 为什么放在IIS 上会报错?
- iis5.1 下如何配置 能使 本机服务器 能够重定向?
- js中用数组加for循环控制文本框值
- 请问怎样将EXCEL中的B5,F9..的值抓出来.(在线等)
- sql得到重复记录,如何解决
- 写asp和asp.net有个疑问........
- 从哪能查询javascript的属性和方法?
- asp.net与oracle8i数据库连接问题
- 已达到系统管理员配置的最大报表处理作业数限制
- 弱制问题,水晶报表的页头怎么显示中文
- 请教 ComponentArt TreeView 节点展开后选中问题
- 为什么DataReader读ACCESS数据库效率那么低?
后面加一行
Create2.Connection = db1;
--->
SqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)", db1);就是说你要指定SqlCommand的Connection属性