111,126
社区成员
发帖
与我相关
我的任务
分享 //连接字符串
ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360data\重要数据\我的文档\用户注册信息.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True";
//创建连接对象
SqlConnection sqlCon = new SqlConnection(ConnectionStr);
//建立插入命令字符串
insertStr = "INSERT INTO 注册信息保存 (username,password,e-mail) VALUES ('"
+ TextBox1.Text + "','" + TextBox2.Text
+ "','"+TextBox4.Text+"')";
try
{
//打开数据
sqlCon.Open();
if (sqlCon.State == ConnectionState.Open)
{
//创建命令对象
SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
}
}
catch
{
if (sqlCon.State != ConnectionState.Open)
{
}
}
finally
{
//关闭数据库
sqlCon.Close();
}using(var sqlCon = new SqlConnection(ConnectionStr)) try
{
sqlCon.Open();
SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
sqlComm.ExecuteNoneQuery();
}足够了。insertStr = "INSERT INTO 注册信息保存 (username,password,e-mail) VALUES ('"
+ TextBox1.Text.Replace("'","''") + "','" + TextBox2.Text.Replace("'","''")
+ "','"+TextBox4.Text.Replace("'","''")+"')";才对。但是这本来应该是常识,你把它另类说成是“麻烦的“‘ ”’符号”那就体现出非常缺乏必要的sql知识了。catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
} 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\重要数据\我的文档\用户注册信息.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True";
//创建连接对象
SqlConnection sqlCon = new SqlConnection(ConnectionStr);
//建立插入命令字符串
insertStr = "INSERT INTO 注册信息保存 ([username],[password],[e-mail]) VALUES (@username,@password,@email)";
try
{
//打开数据
if (sqlCon.State != ConnectionState.Open)
{
sqlCon.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
{
}
finally
{
//关闭数据库
sqlCon.Close();
}
}
是不是我语句位置放错了?应该放哪?