protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=VIVID-PC;Integrated Security = True;Database=SchoolDb");
SqlCommand myCommand = new SqlCommand("Command String", myConnection);
myConnection.Open();
string firstText = TextBox1.Text;
string SecondText = TextBox2.Text;
string thirdText = TextBox3.Text;
string fourthText = TextBox4.Text;
myCommand = new SqlCommand("INSERT INTO SchoolDb_Student(StudentName,RollNo,Session,MobileNo)values('" + firstText + "','" + SecondText + "' , '" + thirdText + "','" + fourthText + "')", myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Redirect("/view.aspx");
}
Q
当唯一键违反
1
A
回答
1
- Use command with parameters to pass data to server。
- 确保你处理连接和命令(via using statement)
- Store connection strings in config file
- 不要创建虚拟命令对象
下面是完整的代码:
using(var connection = new SqlConnection(connectionString))
using(var command = connection.CreateCommand())
{
command.CommandText =
@"INSERT INTO SchoolDb_Student(StudentName,RollNo,Session,MobileNo)
VALUES (@studentName, @rollNo, @session, @mobileNo)";
command.Parameters.AddWithValue("studentName", TextBox1.Text);
command.Parameters.AddWithValue("rollNo", TextBox2.Text);
command.Parameters.AddWithValue("session", TextBox3.Text);
command.Parameters.AddWithValue("mobileNo", TextBox4.Text);
connection.Open();
try
{
command.ExecuteNonQuery();
}
catch(SqlException e)
{
if (e.Message.Contains("Violation of UNIQUE KEY constraint"))
// you got unique key violation
}
}
进一步考虑 - 改善命名的你的代码 - TextBox1,TextBox2等对读者没有任何意义。给他们适当的名字,比如StudentNameTextBox,RollNoTextBox等。另外一个好的做法是将数据访问和UI逻辑分开。
+1
谢谢你的建议兄弟。 – Ashiq
0
如果数据库检测到唯一键冲突如何得到警报,这一行
myCommand.ExecuteNonQuery();
会抛出异常。您可以捕获该异常并继续处理您自己的代码:
try
{
myCommand.ExecuteNonQuery();
}
catch(Exception e)
{
// right here, "something" went wrong. Examine e to check what it was.
}
请注意,您的代码容易受到SQL注入攻击。您应该使用命令参数而不是手动构建SQL。另外,你应该使用using
块(see here for details)
0
0
使用您的returnType from ExecuteNonQuery()(阅读备注部分)来检测插入失败。你可以使用例外或不。行患部
的试试这个:
try
{
... your rest of the code
...
int rowsAffected = myCommand.ExecuteNonQuery(); // Most probaboly it will throw exception in case of Unique key violation. If not, still no rows have been affected
if(rowsAffected<1)
{
//your Alert for no records inserted
}
else
{
//your alert for successful insertion
}
}
catch(SqlException ex)
{
//check the exception and display alert
}
finally
{
//release connection and dispose command object
}
0
正如评论use命令PARAM建议。
try
{
//Your other code
_myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Redirect("/view.aspx");
}
catch(SqlException sqlExc)
{
// Your popup or msg.
}
您还循环查找catch块中的不同sql错误。
相关问题
- 1. 违反唯一键约束
- 2. DbUpdateException违反唯一键
- 3. Django的关键违反唯一约束
- 4. 重复键值违反唯一约束
- 5. 唯一违规:7错误:重复键值违反唯一约束“users_pkey”
- 6. 违反唯一约束?
- 7. django“重复的键值违反了唯一约束”主键
- 8. 违反外键
- 9. 当我有一个非唯一索引时违反了唯一约束条件
- 10. PG :: UniqueViolation:错误:重复键值违反唯一约束
- 11. php Postgresql pg_query():重复键值违反了唯一性约束
- 12. 使用EF插入行时违反唯一键约束4
- 13. Django的:IntegrityError:重复键值违反唯一约束
- 14. Django - 重复键值违反了唯一约束条件
- 15. 即使没有主键oracle也会违反唯一约束
- 16. 休眠:重复键值违反唯一约束
- 17. 如何解决django.db.utils.IntegrityError:重复键值违反唯一约束?
- 18. PostgreSQL的:错误重复键值违反唯一约束
- 19. 错误:重复的键值违反了唯一约束“xak1fact_dim_relationship”
- 20. 休眠 - 重复键值违反唯一约束
- 21. Django的:重复键值违反唯一约束
- 22. LEFT OUTER JOIN导致违反唯一键约束
- 23. 错误:重复键违反唯一约束“search6_idx1”
- 24. 重复的键值违反了唯一的约束,CakePHP
- 25. django.db.utils.IntegrityError:重复键值违反唯一约束“django_content_type_pkey”
- 26. Django的:重复键值违反唯一约束
- 27. django.db.utils.IntegrityError:重复键值违反唯一约束“auth_permission_pkey”
- 28. 违反了“唯一粒子属性”
- 29. ORA-00001:违反的唯一约束(DEV.X_PK)
- 30. 合并导致违反唯一约束
检测到[SQL Injection](http://www.w3schools.com/sql/sql_injection.asp)。使用命令参数 –
对不起,我不能让你。具体帮助我,我是一个初学者。 – Ashiq
你可以点击链接 –