不知道为什么,数据写不进数据库,数据库里没数据。希望哪位好心人给解答。using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class 注册页面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
{
Label1.Text = "注册信息没有填写完整";
} else
{
Response.Redirect("Default.aspx");
}
string ConnectionStr, insertStr; //连接字符串
ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360DATA\重要数据\我的文档\VISUAL STUDIO 2008\期末作业\APP_DATA\用户信息.MDF;
Integrated Security=True;Connect Timeout=30;User Instance=True";
//创建连接对象
SqlConnection sqlCon = new SqlConnection(ConnectionStr);
//建立插入命令字符串
insertStr = "INSERT INTO 注册信息保存 ([username],[password],[email]) VALUES (@username,@password,@email)";
try
{ //打开数据
sqlCon.Open();
if (sqlCon.State == ConnectionState.Open)
{
//创建命令对象
SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
sqlComm.Parameters.AddWithValue("@username", TextBox1.Text);
sqlComm.Parameters.AddWithValue("@password", TextBox2.Text);
sqlComm.Parameters.AddWithValue("@email", TextBox4.Text);
sqlComm.ExecuteNonQuery(); }
}
catch (SqlException exp)
{
Response.Write(exp.ToString());
}
finally
{
//关闭数据库
sqlCon.Close();
}
}}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class 注册页面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
{
Label1.Text = "注册信息没有填写完整";
} else
{
Response.Redirect("Default.aspx");
}
string ConnectionStr, insertStr; //连接字符串
ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360DATA\重要数据\我的文档\VISUAL STUDIO 2008\期末作业\APP_DATA\用户信息.MDF;
Integrated Security=True;Connect Timeout=30;User Instance=True";
//创建连接对象
SqlConnection sqlCon = new SqlConnection(ConnectionStr);
//建立插入命令字符串
insertStr = "INSERT INTO 注册信息保存 ([username],[password],[email]) VALUES (@username,@password,@email)";
try
{ //打开数据
sqlCon.Open();
if (sqlCon.State == ConnectionState.Open)
{
//创建命令对象
SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
sqlComm.Parameters.AddWithValue("@username", TextBox1.Text);
sqlComm.Parameters.AddWithValue("@password", TextBox2.Text);
sqlComm.Parameters.AddWithValue("@email", TextBox4.Text);
sqlComm.ExecuteNonQuery(); }
}
catch (SqlException exp)
{
Response.Write(exp.ToString());
}
finally
{
//关闭数据库
sqlCon.Close();
}
}}
错误信息
insertStr = "INSERT INTO [注册信息保存]([username],[password],[email]) VALUES (@username,@password,@email)";
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
{
Label1.Text = "注册信息没有填写完整";
} string ConnectionStr, insertStr; //连接字符串
ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360DATA\重要数据\我的文档\VISUAL STUDIO 2008\期末作业\APP_DATA\用户信息.MDF;
Integrated Security=True;Connect Timeout=30;User Instance=True";
//创建连接对象
SqlConnection sqlCon = new SqlConnection(ConnectionStr);
//建立插入命令字符串
insertStr = "INSERT INTO 用户信息保存 ([username],[password],[email]) VALUES (@username,@password,@email)";
//打开数据
sqlCon.Open();
if (sqlCon.State == ConnectionState.Open)
{
//创建命令对象
SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
sqlComm.Parameters.AddWithValue("@username", TextBox1.Text);
sqlComm.Parameters.AddWithValue("@password", TextBox2.Text);
sqlComm.Parameters.AddWithValue("@email", TextBox4.Text);
sqlComm.ExecuteNonQuery(); }
//关闭数据库
sqlCon.Close();
if (sqlCon.State != ConnectionState.Open)
{
Response.Redirect("Default.aspx");
}真心累啊,原来是条件语句导致数据存不进去,果然跟我最初想的一样,我把else去掉了终于解决了,哈哈。