在我将CustID int和自动增量放入数据库后,它会出错...但是我删除它之后...可以保存在数据库中...现在的问题我想在int或primarykey中添加CustID ..如何弹出/显示消息,如项目已保存?
这是我的SQL数据库..
Custid int (could not work) help
custname varchar50
custadd varchar50
custphone varchar
custemail varchar50
这些是我的代码:
public partial class NewCase : System.Web.UI.Page
{
SqlConnection con =new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\WebSite6\\App_Data\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand("Select * from aspnet_Customer");
public void Bind()
{
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
da.Fill(ds, "aspnet_Customer");
con.Close();
}
protected void Button1_Click1(object sender, System.EventArgs e)
{
con.Open();
SqlCommand comi = new SqlCommand("Insert into aspnet_Customer values (@Custname,@Custaddress,@Custemail,@Custphone)");
comi.Parameters.Add("Custname", SqlDbType.VarChar, 50);
comi.Parameters["Custname"].Value = TextBox3.Text;
comi.Parameters.Add("Custaddress", SqlDbType.VarChar, 50);
comi.Parameters["Custaddress"].Value = TextBox9.Text;
comi.Parameters.Add("Custemail", SqlDbType.VarChar, 50);
comi.Parameters["Custemail"].Value = TextBox10.Text;
comi.Parameters.Add("Custphone", SqlDbType.VarChar, 50);
comi.Parameters["Custphone"].Value = TextBox11.Text;
comi.Connection = con;
comi.ExecuteNonQuery();
con.Close();
Message.box("DATA ADDED SUCCESSFULLY!!");
Bind();
}
}
答案 0 :(得分:1)
如果您有自动增量列,则必须在INSERT INTO
子句中列出列名,因此您无法插入所有列(您的自动增量列会自动生成)。
SqlCommand comi = new SqlCommand(
"Insert into aspnet_Customer( Custname, Custaddress, Custemail, Custphone)
values (@Custname,@Custaddress,@Custemail,@Custphone)");