我目前正在开展一个项目,我需要将文件路径提交到数据库,并且还能够从数据库中提取该路径。
目前我可以将路径提交到数据库并从数据库中提取数据,但是当我提交到数据库时,路径中的斜线似乎从字符串中删除了。
在提交到数据库和从数据库中提取时,如何阻止\被删除?
这是我的保存按钮事件代码:
private void btnSaveSettings_Click(object sender, EventArgs e)
{
string myConnection = "datasource=localhost;port=3306;username=username;password=password";
string Query = "insert into ephex_contcollections.Paths (toindex,indexed) values('" + this.txtToIndexPath.Text + "','" + this.txtIndexedPath.Text + "') ;";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDB = new MySqlCommand(Query, myConn);
MySqlDataReader myReader;
try
{
myConn.Open();
myReader = cmdDB.ExecuteReader();
MessageBox.Show("Save Successful");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} //close save button event
我提交给数据库的字符串在文本框中看起来像这样:
C:\Users\Meta\Desktop\Alpha
但是他们像这样提交给数据库:
C:UsersMetaDesktopAlpha
在此先感谢(我是使用 C# 使用 MySQL 数据库的新手,我的 C# 也很生疏)。
最佳答案
你应该使用 SQL Parameters ,但如果你想快速解决这个问题,请在路径中添加双斜杠,这样输出将是:C:\\Users\\Meta\\Desktop\\Alpha
string Query = "insert into ephex_contcollections.Paths (toindex,indexed) values('" + this.txtToIndexPath.Text.Replace(@"\" , @"\\") + "','" + this.txtIndexedPath.Text.Replace(@"\" , @"\\") + "') ;";
关于c#提交到数据库时从字符串中剥离字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930786/