使用数据库在winform中更新命令的问题 [英] Problem with update command in winform with database

查看:57
本文介绍了使用数据库在winform中更新命令的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在winform中使用数据库在sql server数据库中创建插入更新删除

问题是当我选择我更新的数据并单击更新按钮时它显示我的列名无效?

任何人都可以帮助我解决这个问题



我尝试了什么:



 private void updatebtn_Click(object sender,EventArgs e)
{
{
try
{
if( comboBox1.Text ==)
{
MessageBox.Show(输入要更新的军队号码);
}
else
{
SqlCommand cmdupdate = new SqlCommand(Update insupdel SET name ='+ textBox1.Text +',rank ='+ comboBox2.SelectedItem + ',unit ='+ comboBox3.SelectedItem +',subunit ='+ comboBox4.SelectedItem +',medicalcategory ='+ comboBox5.SelectedItem +',category ='+ comboBox6.SelectedItem +', datetimepicker1 ='+ dateTimePicker1.Text +'其中armynumber =+ comboBox1.SelectedItem +,con);
con.Open();
cmdupdate.CommandType = CommandType.Text;
cmdupdate.ExecuteNonQuery();
MessageBox.Show(Data Updated);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
最后
{
if(con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}

解决方案

Google for SQL注入攻击找出你正在做的事情为何如此糟糕。



然后谷歌为C#SQL参数化查询找出如何解决这个问题并且可能同时修复你的问题。


不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



当你在整个应用程序中修复它时,问题可能会消失


created insert update delete in winform with database in sql server database
the problem is when i select the my updated data and click on update button it is showing me that invalid column name?
can anyone plz help me in solving this problem

What I have tried:

private void updatebtn_Click(object sender, EventArgs e)
      {
          {
              try
              {
                  if (comboBox1.Text=="")
                  {
                      MessageBox.Show("enter army number to update");
                  }
                  else
                  {
                      SqlCommand cmdupdate = new SqlCommand("Update insupdel SET name='" + textBox1.Text + "',rank='" + comboBox2.SelectedItem + "' ,unit='" + comboBox3.SelectedItem + "', subunit='" + comboBox4.SelectedItem + "', medicalcategory='" + comboBox5.SelectedItem + "', category='" + comboBox6.SelectedItem+ "',datetimepicker1='"+dateTimePicker1.Text+"'  where armynumber=" + comboBox1.SelectedItem + "", con);
                      con.Open();
                      cmdupdate.CommandType = CommandType.Text;
                      cmdupdate.ExecuteNonQuery();
                      MessageBox.Show("Data Updated");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
              finally
              {
                  if (con.State == ConnectionState.Open)
                  {
                      con.Close();
                  }
              }
          }
      }

解决方案

Google for "SQL Injection Attack" to find out why what you're doing is so bad.

Then Google for "C# SQL parameterized queries" to find out how to fix that and probably fix your problem at the same time.


Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The chances are that when you've fixed that throughout your whole app, the problem will have gone away as well.


这篇关于使用数据库在winform中更新命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆