管理从Windows窗体到mysql的转义字符 [英] managing escape character from windows form to mysql

查看:63
本文介绍了管理从Windows窗体到mysql的转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个Windows窗体,其中包含一个文本框,在该文本框中,我将输入路径,单击按钮后,我会将数据绑定到数据库(mysql),然后用它完成.但是,如果输入了路径包含"\"(转义字符),例如,如果E:\ folder是path,它将在绑定时被忽略,它将作为E:folder输入数据库,我知道使用@可以解决问题,但我仍然无法执行此操作,我正在使用属性读取n写文件路径,我可以在属性本身中附加@吗?还是有其他方法可以执行此操作?

i have designed a windows forms which has a textbox in it,in that textbox i will enter path and on click of button i will bind that data to database(mysql) and i am done with it.but if the entered path contains ''\''(escape character) it will be ignored while binding for e.g, if E:\folder is path it will be entered as E:folder into database,i know using @ would solve the problem,but still i am not able to do that,i am using property to read n write the file path,can i append @ in the property itself??or is there any other way to do that??

public stringFilePath
        {
            get {return this.FilePathTextBox.Text.Trim();}
            set {this.FilePathTextBox.Text = value;}
        }

推荐答案



根据以下网站: MySql字符串,您可以使用:' '\\''->反斜杠(''\'')字符.''\\''

我认为这可以解决您的问题.

亲切的问候,
Hi,

According to the following web site: MySql Strings you can make use of: ''\\'' -> A backslash (''\'') character.''\\''

I think that would solve your problem.

Kind regards,


不幸的是,@是常量字符串.
您需要自己对字符串进行转义...
Unfortunately, @ is for constant strings.
You need to escape the string yourself...


您可以使用Replace像这样将"\"转换为"\\"

You can just use Replace to convert "\" to "\\" like this

public string FilePath
        {
            get{return textBox1.Text.Trim();}
            set { this.textBox1.Text = value.Replace(@"\", @"\\"); }
        }



希望对您有帮助

这是我用于测试的整个表单的代码:



Hope this Helps

Here is the code for the whole form I used for testing :

public string FilePath
        {
            get{return textBox1.Text.Trim();}
            set { this.textBox1.Text = value.Replace(@"\", @"\\"); }
        }
        public Form1()
        {
            InitializeComponent();
        }
        public void Save()
        {
            using (MySqlConnection con = new MySqlConnection("server=localhost;User Id=xxxx;password=xxxx;database=xxxx"))
            {
                MySqlCommand cmdSave = new MySqlCommand("UPDATE"
                                                          + " myTable"
                                                  + " SET"
                                                         + " FilePath = @FilePath"
                                                   + " WHERE"
                                                         + " myClassID = @myClassID", con);
                cmdSave.Parameters.AddWithValue("@myClassID", 1);
                cmdSave.Parameters.AddWithValue("@FilePath", FilePath);
                con.Open();
                int rowsAffected = cmdSave.ExecuteNonQuery();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Save();
        }



一切都按预期进行.

祝你好运!



and it all works as expected.

Good Luck!!


这篇关于管理从Windows窗体到mysql的转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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