插入到mysql数据库时,C#文本框不包含反斜杠 [英] C# textbox not contains backslash while insert into mysql database

查看:198
本文介绍了插入到mysql数据库时,C#文本框不包含反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我希望我能告诉你我的问题。



我无法将文件路径存储到C#中的mysql。当我插入图像数据库时,路径不包含反斜杠。



我尝试过:



这是我的代码:
< pre>
MySqlConnection baglan = BaglantiNesnesiVer();
baglan.Open();


string str = textBox5.Text;
string str2 = textBox3.Text;
string str3 = textBox2.Text;
string substr = str.Substring(str.Length - 3);
string substr2 = str2.Substring(str2.Length - 3);
string substr3 = str3.Substring(str3.Length - 2);
textBox6.Text = substr + substr2 + substr3;
Zen.Barcode.Code128BarcodeDraw条形码= Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
pictureBox2.Image = barcode.Draw(textBox6.Text,50);
string resim = Path.GetFullPath(@ textBox7.Text);
MySqlCommand ekle = new MySqlCommand(插入kayit(Sifre,TC,AdSoyad,DogumTarihi,Cinsiyeti,VeliAd,VeliTel,EPosta,Yakinlik,Adres,Resim)值('+ textBox6.Text +',' + textBox5.Text +','+ textBox1.Text +','+ dateTimePicker1.Text +','+ comboBox2.Text +','+ textBox2.Text +',' + textBox3.Text +','+ textBox4.Text +','+ comboBox1.Text +','+ richTextBox1.Text +','+ resim +'),baglan );
ekle.ExecuteNonQuery();
baglan.Close();
MessageBox.Show(Kaydınızbaşarıylagerçekleşmiştir.Kartişlemlerinigerçekleştirmeyiunutmayınız!,BilgiMesajı);





resim是我的文件路径。像这样出现在mysql中

 C:UsersPublicPicturesSamplePicturesLighthouse.jpg 





为什么我不能存储C:\\Users \ PublicPictures \ SamplePictures\Lighthouse.jpg

解决方案

不要这样做!永远不要连接字符串来构建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; 

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

   -   ' 

其他一切都是评论。

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



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



修复整个应用程序,你注意到的问题也会消失......


我这样解决了谢谢。

 string resim = textBox7.Text; 
string yol = resim.Replace(\\,\\\\);


Hi everybody. I hope i can tell you my problem.

i cant store filepath to mysql in C# . when i insert into image database, path is not contains backslash.

What I have tried:

this is my code:
<pre>
            MySqlConnection baglan = BaglantiNesnesiVer();
            baglan.Open();


            string str = textBox5.Text;
            string str2 = textBox3.Text;
            string str3 = textBox2.Text;
            string substr = str.Substring(str.Length - 3);
            string substr2 = str2.Substring(str2.Length - 3);
            string substr3 = str3.Substring(str3.Length - 2);
            textBox6.Text = substr + substr2 + substr3;
            Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
            pictureBox2.Image = barcode.Draw(textBox6.Text, 50);
            string resim = Path.GetFullPath(@textBox7.Text);
            MySqlCommand ekle = new MySqlCommand("insert into kayit(Sifre,TC,AdSoyad,DogumTarihi,Cinsiyeti,VeliAd,VeliTel,EPosta,Yakinlik,Adres,Resim) values('" + textBox6.Text + "','" + textBox5.Text + "','" + textBox1.Text + "','" + dateTimePicker1.Text + "','" + comboBox2.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + comboBox1.Text + "','" + richTextBox1.Text + "','" + resim + "')", baglan);
            ekle.ExecuteNonQuery();
            baglan.Close();
            MessageBox.Show("Kaydınız başarıyla gerçekleşmiştir.Kart işlemlerini gerçekleştirmeyi unutmayınız!", "Bilgi Mesajı");



resim is my file path.its appear in mysql like this

C:UsersPublicPicturesSamplePicturesLighthouse.jpg



why i cant store C:\\Users\PublicPictures\SamplePictures\Lighthouse.jpg

解决方案

Don't do it 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?

Fix that throughout the whole application, and the problem you noticed will go away as well...


i solved this way thank you.

string resim = textBox7.Text;
string yol = resim.Replace("\\", "\\\\");


这篇关于插入到mysql数据库时,C#文本框不包含反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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