帮忙看看C#代码

来源:百度知道 编辑:UC知道 时间:2024/05/31 03:08:56
protected void btnClick_Click(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=B;Initial Catalog=facebook;Integrated Security=True");
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into facebook";
cmd.CommandText += "(fbName,fbSex,fbPhoneNumber,fbBirthday,fbEmail,fbAddress,fbGroup)values(";
cmd.CommandText += "'" + txtName.Text + "','" + dropSex.SelectedItem.Text+"','" + txtPhoneNumber.Text + "','" + txtBirthday.Text;
cmd.CommandText += "','" + txtEmail.Text + "','" + "','" + txtAddress.Text + "','" + dropGroup.SelectedItem.Value.ToString();
cmd.CommandText += "')";
cmd.ExecuteNonQuery();
Response.R

要插入的字段就7个(fbName,fbSex,fbPhoneNumber,fbBirthday,fbEmail,fbAddress,fbGroup);
你values()后面的不止7个啊
问题就在这里。象这种出问题的SQL语句,你先用Response.write(sql);的方式输出SQL,再到SQL查询分析器里面执行,如果成功就行了啊。

protected void btnClick_Click(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=B;Initial Catalog=facebook;Integrated Security=True");
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
string sql = "insert into facebook(fbName,fbSex,fbPhoneNumber,fbBirthday,fbEmail,fbAddress,fbGroup) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')";
sql = string.Format(sql,txtName.Text,dropSex.SelectedItem.Text,txtPhoneNumber.Text,txtBirthday.Text,txtEmail.Text,txtAddress.Text,dropGroup.SelectedItem.Value.ToString())
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Response.Redirect("dataview.aspx");

//你那样写的错误不是参数少了.是values前面少了个空格,不过最好这样