问题描述
我设计了一个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;}
}
推荐答案
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的转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!