注册 登录
编程论坛 ASP.NET技术论坛

为什么添加不了数据呢

apple0129 发布于 2008-04-20 22:08, 744 次点击
我通过一个添加按钮向数据库中添加数据,不报错,但是数据库中没有数据,是怎么回事啊
我的代码如下:
private void btnadd_Click(object sender, System.EventArgs e)
        {   SqlConnection sqlcon = new SqlConnection("server=.;database=peisong; uid=sa;pwd=;");//创建SqlConnection对象
            sqlcon.Open();
            SqlCommand cmd = new SqlCommand();
         into user values (@username,@storename,@post,@powerid,@dlname,@pwd,"
            +"@sex,@birthday,@cardid,@ustelephone,@usaddress)";
        cmd.Parameters.Add("@username",username.Text.ToString());
        cmd.Parameters.Add("@storename",this.store.SelectedItem.Value);
        cmd.Parameters.Add("@post",this.post.SelectedItem.Value);
        cmd.Parameters.Add("@powerid",this.power.SelectedItem.Value);
        cmd.Parameters.Add("@dlname",dlname.Text.ToString());
        cmd.Parameters.Add("@pwd",pwd.Text.ToString());
        cmd.Parameters.Add("@sex",this.sex.SelectedItem.Value);
        cmd.Parameters.Add("@birthday",DateTime.Now);
        cmd.Parameters.Add("@cardid",cardid.Text.ToString());
        cmd.Parameters.Add("@ustelephone",tel.Text.ToString());
        cmd.Parameters.Add("@usaddress",address.Text.ToString());
            cmd.Connection=sqlcon;
            cmd.ExecuteNonQuery();
            sqlcon.Close();
4 回复
#2
残影留香2008-04-21 09:26
你这个commandType属性没有设置
#3
guaishi2008-04-22 17:23
我也添加不了数据,没有报任何错误码
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
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
using
using System.Text;


public partial class support : System.Web.UI.Page
{

    protected void ibt_Click(object sender, ImageClickEventArgs e)
    {
        string kkwd = kwd.Text.Trim().Replace("’", "'");
        if (kkwd != string.Empty || kkwd != null)
        {
            Response.Redirect("ppsearch.aspx?kwd=" + kkwd);
        }
        else
        {

        }
    }

    protected void tijiao_Click(object sender, EventArgs e)
    {
        //string company = Request["company"];
        //string first = Request["first"];
        //string last = Request["last"];
        //string phone = Request["phone"];
        //string fax = Request["fax"];
        //string email = Request["email"];
        //string product = Request["product"];
        //string type = Request["type"];
        //string title = Request["tile"];
        //string message = Request["message"];

        string cons = "Provider=Microsoft.Jet.OleDb.4.0;";
        // Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\website1\#db1.aspx;Persist Security Info=False
        cons += "Data Source=";
        cons += MapPath("#db1.aspx");
        cons += ";Persist Security Info=False";
        try
        {
            OleDbConnection scon = new OleDbConnection(cons);
            scon.Open();
            OleDbCommand scom = new OleDbCommand();
            scom.Connection = scon;
             = "Insert into forback(Company,First,Last,Phone,Fax,Email,Product,Type,Title,Message)values('" + company.Text + "','" + first.Text + "','" + last.Text + "','" + phone.Text + "','" + fax.Text + "','" + email.Text + "','" + product.Text + "','" + tile.Text + "','" + type.Items + "''" + message.Text + "')";
            //OleDbCommand odd = new OleDbCommand(selstr, odc);
            scom.ExecuteNonQuery();
            Response.Redirect("index.aspx");
            int count = scom.ExecuteNonQuery();

            if (count > 0)
            {
                //MessageBox.Show("添加成功!");
                scon.Close();
                Response.Redirect("index.aspx");
            }
            else
            {
               
                //MessageBox.Show("添加失败!");
            }
        }

        catch (Exception ee)
        {
            //MessageBox.Show(ee.Message.ToString());
        }
    }
}
#4
yiyihui822008-04-22 18:09
断点调试一下, 都运行了吗?
#5
guaishi2008-04-27 12:37
都可以运行
1