我有一些问题:下面的代码用新行更新了表格,但是这一行中的所有字段都是空的。我想将文本字段中的输入数据放入成员表中的新行中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtAdress.Text = "";
txtResidence.Text = "";
txtEmail.Text = "";
}
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);
cmd.Parameters.AddWithValue("@txtFirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@txtLastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@txtAdress", txtAdress.Text);
cmd.Parameters.AddWithValue("@txtResidence", txtResidence.Text);
cmd.Parameters.AddWithValue("@txtEmail", txtEmail.Text);
try
{
con.Open();
cmd.ExecuteNonQuery();
lblConfirm.Text = "Database updated, insert confirmed!";
txtFirstName.Text = "";
txtLastName.Text = "";
txtAdress.Text = "";
txtResidence.Text = "";
txtEmail.Text = "";
}
finally
{
con.Close();
}
}
}
如果我改变这一行:
SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);
对于这样的事情:
SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES ('@txtFirstName', '@txtLastName', @txtAdress, @txtResidence, @txtEmail)", con);
它会打印出一行这样的字段:| @txt名字 | @txt姓氏| [空] | [空] | [空] |
任何帮助都会很棒,谢谢!