我有一个 C# 程序,允许用户发送和接收文本消息,接收传入文本的服务器解析消息,然后将它们放入 SQL 数据库中,我想知道是否有一种方法可以检测新行的插入进入该数据库,并触发一个事件来通知用户有新的传入消息?
我正在编写一个应用程序来将文本存储到SQL数据库,但是我的代码抛出一个异常,说"变量名@ par1已经被声明了",我不知道如何使这个工作,并希望一些帮助解决这个问题请尽可能请=]
违规代码如下
private void SMSGetter()
{
try {
DecodedShortMessage[] messages = Comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
SqlConnection Conn = new SqlConnection("Data Source=*********;Initial Catalog=********;User ID=**********;Password=***********");
SqlCommand com = new SqlCommand();
com.Connection = Conn;
Conn.Open();
foreach (DecodedShortMessage message in messages)
{
//com.CommandText = ("INSERT INTO SMSArchives(Message,Blacklist) VALUES ('" + message.Data.UserDataText + "', 'Yes')");
//com.ExecuteNonQuery();
com.CommandText = ("INSERT INTO SMSArchives(Message,Blacklist) VALUES (@par1,@par2)");
com.Parameters.AddWithValue("@par1", message.Data.UserDataText);
com.Parameters.AddWithValue("@par2", "Yes");
com.ExecuteNonQuery();
}
Conn.Close();
}
catch (Exception ex) {
Log(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 我正在c#winforms中创建用户登录屏幕,我希望能够根据此链接检查用户的用户名和密码以防止SQL数据库中的记录,但是我的代码会抛出一个异常,说"用户附近的语法不正确".
任何人都可以帮我弄清楚我的代码有什么问题吗?违规代码如下.
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
private void LoginBtn_Click(object sender, EventArgs e)
{
//var username = textBox1.Text;
//var password = maskedTextBox1.Text;
try
{
SqlConnection Conn = new SqlConnection("Data Source=***********;Initial Catalog=*********;Persist Security Info=True;User ID=*********;Password=*******");
SqlCommand com = new SqlCommand();
com.Connection = Conn;
Conn.Open();
com.CommandText = ("SELECT (Username) AS User, (Password) as Pass FROM dbname WHERE User='" + textBox1.Text + "'");
SqlDataReader reader …Run Code Online (Sandbox Code Playgroud)