protected void Button1_Click(object sender, EventArgs e)
{
string connectstring = @"server=CSDN-3F5C293478\SQLEXPRESS;database=website;integrated security=true;";
SqlConnection thisconnection = new SqlConnection(connectstring);
if (TextBox1.Text == "" ¦ ¦ TextBox2.Text == "")
{
Response.Write(" <script>alert('用户名或者密码不能为空') </script>");
}
else
{
try
{
string sql = "insert into dbo.User (UserName,UserId,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
SqlCommand cmd = new SqlCommand(sql, thisconnection);
thisconnection.Open();
cmd.ExecuteNonQuery();
Response.Write(" <script>windows.location.href='Index.aspx';alert('注册成功'); </script>");
}
catch(SqlException ee)
{
Response.Write(ee.Message);
//Response.Write(" <script>alert('数据库读取错误,请重新输入') </script>");
}
finally
{
thisconnection.Close();
}
}
}
这是asp.net注册登录时的注册按钮的处理事件,连接数据库没有问题。但是当到了红色的这行时,捕捉到了异常,说是User附近有语法错误,一直不能往数据库里面添加记录,我检查了命令没发现有错误啊?这到底是怎么回事?已经发过一遍了,可是答案我试了都不对,不知道为什么
{
string connectstring = @"server=CSDN-3F5C293478\SQLEXPRESS;database=website;integrated security=true;";
SqlConnection thisconnection = new SqlConnection(connectstring);
if (TextBox1.Text == "" ¦ ¦ TextBox2.Text == "")
{
Response.Write(" <script>alert('用户名或者密码不能为空') </script>");
}
else
{
try
{
string sql = "insert into dbo.User (UserName,UserId,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
SqlCommand cmd = new SqlCommand(sql, thisconnection);
thisconnection.Open();
cmd.ExecuteNonQuery();
Response.Write(" <script>windows.location.href='Index.aspx';alert('注册成功'); </script>");
}
catch(SqlException ee)
{
Response.Write(ee.Message);
//Response.Write(" <script>alert('数据库读取错误,请重新输入') </script>");
}
finally
{
thisconnection.Close();
}
}
}
这是asp.net注册登录时的注册按钮的处理事件,连接数据库没有问题。但是当到了红色的这行时,捕捉到了异常,说是User附近有语法错误,一直不能往数据库里面添加记录,我检查了命令没发现有错误啊?这到底是怎么回事?已经发过一遍了,可是答案我试了都不对,不知道为什么
{
string connectstring = @"server=CSDN-3F5C293478\SQLEXPRESS;database=website;integrated security=true;";
SqlConnection thisconnection = new SqlConnection(connectstring);
if (TextBox1.Text == "" || TextBox2.Text == "")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>alert('用户名或者密码不能为空')</script>");
}
else
{
try
{
string sql = "insert into [User] (UserName,UserId,Email) values(@UserName,@UserId,@Email)";
SqlCommand cmd = new SqlCommand(sql, thisconnection);
cmd.Parameters.AddWithValue("@UserName", TextBox1.Text);
cmd.Parameters.AddWithValue("@UserId", TextBox2.Text);
cmd.Parameters.AddWithValue("@Email", TextBox3.Text);
thisconnection.Open();
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>windows.location.href='Index.aspx';alert('注册成功'); </script>");
}
catch(SqlException ee)
{
Response.Write(ee.Message);
//Response.Write(" <script>alert('数据库读取错误,请重新输入') </script>");
}
finally
{
thisconnection.Close();
}
}
} 如果有错,报什么错误信息
应该是这个问题吧
错误
SELECT * FROM DBO.USER
正确:
SELECT * FROM DBO.[USER]
这样试过吗
insert into [user](User_Name,User_NickName,User_Pwd,User_Integral) values('111','wo','111','1')
insert into NewHotel(你的數據庫名字).dbo.[user](User_Name,User_NickName,User_Pwd,User_Integral) values('111','wo','111','1')完全可以執行 你設置斷點 將你的sql 語句 複製出來看看