我想写一上能用页面输入内容来创建数据表的页,发现了一个问题,请大家指教!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 属性尚未初始化。

解决方案 »

  1.   

    trySqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)");
    后面加一行
    Create2.Connection = db1;
      

  2.   

    或者是这样改也可以SqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)");
    --->
    SqlCommand Create2 = new SqlCommand("Create Table " + TableName1 + "(" + LieName1 + " int(4)primary key)", db1);就是说你要指定SqlCommand的Connection属性
      

  3.   

    呵呵,这个问题是解决了,他又提示:第 1 个列或参数: 不能对数据类型 int 指定列宽度。我把int(4)primary key   中的(4)  去掉了,就可以了,为什么不能对 int 指定列宽度。int(4)举的是列宽吗?
      

  4.   

    int类型就是占用4个字节,不用指定,也不能更改 
      

  5.   

    这个要看具体的数据类型,比如int,datetime,text等等这些占用字节数固定的,是不能指定和更改了,而varchar等是可以更改的,参考一下联机帮助吧
      

  6.   

    好的,能否加下你QQ或邮箱,以后有问题可以在请教请教你!我的QQ是329575813