我已经将SQL数据库连接到aspx.net表单,但是当我在表单中输入详细信息时,它似乎并没有在SQL表中更新。我检查了代码,似乎没有任何错误。谁能看到我的代码出了什么问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
public partial class CustomerLogin : System.Web.UI.Page {
public string sqlTest = "Data Source=TEAFAMILY;Initial Catalog=Bolsen;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e) {
}
static readonly string scriptSuccessNewAccount =
"<script language=\"javascript\">\n" +
"alert (\"Your account has been succesfully created - Thank You!\");\n" +
"</script>";
protected void Button1_Click1(object sender, EventArgs e) {
SqlConnection mDB = new SqlConnection(sqlTest);
mDB.Open();
Type csType = this.GetType();
SqlCommand cmd;
SqlDataReader rdr;
string strSQLSelect = "SELECT cEmail FROM Customers ORDER BY cEmail";
cmd = new SqlCommand(strSQLSelect, mDB);
Console.Write(cmd);
rdr = cmd.ExecuteReader();
//insert new record
string strSQLInsert = "INSERT INTO"
+ " Customers (cFirstname, cLastname, cNumber, cCompanyname, cAdd, cEmail, cPassword)"
+ " VALUES (@FN, @LN, @Num, @Cname, @Add, @Email, @Pw)";
cmd = new SqlCommand(strSQLInsert, mDB);
cmd.Parameters.AddWithValue("@FN", txtFN.Text);
cmd.Parameters.AddWithValue("@LN", txtLN.Text);
cmd.Parameters.AddWithValue("@Num", txtPN.Text);
cmd.Parameters.AddWithValue("@Cname", txtComp.Text);
cmd.Parameters.AddWithValue("@Add", txtCompAdd.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Pw", txtPW.Text);
cmd.ExecuteNonQuery();
mDB.Close();
ClientScript.RegisterStartupScript(csType, "Success", scriptSuccessNewAccount);
}
}
答案 0 :(得分:1)
您没有关闭SqlDataReader
。除了不调用rdr.Read()
并获取任何值之外,您还需要在执行第二条sql语句之前调用rdr.Close()
。
每个MSDN-正在使用SqlDataReader时,关联的SqlConnection忙于为SqlDataReader提供服务,除了关闭它以外,无法对SqlConnection执行其他任何操作。在调用SqlDataReader的Close方法之前,情况就是这样。例如,您必须在调用Close之后才能检索输出参数。