ASP.net中如何将指定的内容添加到SQL数据库?
   
        SqlDataSource sqlnote = new SqlDataSource();
        sqlnote.SelectCommand = "SELECT  * FROM [Notebook] ORDER BY [noteID] DESC";这个是显示表的语句,绑定控件后也显示成功了. 那么我要向此表添加数据的话怎么写代码?
sqlnote.InsertCommand 吗? 然后呢

解决方案 »

  1.   

    我写的代码是sqlnote.InsertCommand = "INSERT INTO [Notebook] ([username], [notetime], [notepage]) VALUES ('hshs','sss','haha'; )";
     断点跟踪后InsertCommand里值是空的
      

  2.   

    这样的例子很多的,我给你搜一个,你可以仿照:
    --------------------------------------------
             SqlDataSource sds = new SqlDataSource();
             sds.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
             sds.InsertCommand = "Insert into T_Product(F_Name,F_Price,F_Unit) values(@Name,@Price,@Unit)";
             //sds.InsertParameters.Add(new Parameter("Name",TypeCode.String,txtName.Text.Trim()));
             //sds.InsertParameters.Add(new Parameter("Price",TypeCode.Decimal,txtPrice.Text.Trim()));
             //sds.InsertParameters.Add(new Parameter("Unit", TypeCode.String, txtUnit.Text.Trim()));
             sds.InsertParameters.Add("Name", txtName.Text.Trim());
             sds.InsertParameters.Add("Price", txtPrice.Text.Trim());
             sds.InsertParameters.Add("Unit", txtUnit.Text.Trim());
             int flag = 0;
             try
             {
                 flag = sds.Insert();
             }
             catch
             {
                 ;
             }
             if (flag == 0)
             {
                 lblInsert.Text = "<span class='Error'>插入失败!</span>";
             }
             else
             {
                 Response.Redirect(Request.ServerVariables["URL"].ToString());
             }
      

  3.   

    看看书吧,这个是很基础的ADO.NET的知识,虽然我也不是很懂