【问题标题】:"Illegal characters in path" error when connecting to SQL Server连接到 SQL Server 时出现“路径中的非法字符”错误
【发布时间】:2013-07-29 15:25:50
【问题描述】:

我正在尝试连接数据库,但出现以下错误:

路径中有非法字符。

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\targil3.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlDataAdapter adapt = new SqlDataAdapter();
    adapt.InsertCommand = new SqlCommand(" INSERT INTO tblEmployee VALUES (@employeeNumber, @employeePrivateName, @employeeFamilyName ,@city, @street, @houseNo, @phoneNumber, @birthDate, @startWorkingDate)", Con);
    adapt.InsertCommand.Parameters.Add("@employeeNumber", SqlDbType.Char).Value = textBox1.Text;
    adapt.InsertCommand.Parameters.Add("@employeePrivateName", SqlDbType.VarChar).Value = textBox2.Text;
    adapt.InsertCommand.Parameters.Add("@employeeFamilyName", SqlDbType.VarChar).Value = textBox3.Text;
    adapt.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar).Value = textBox4.Text;
    adapt.InsertCommand.Parameters.Add("@street", SqlDbType.VarChar).Value = textBox5.Text;
    adapt.InsertCommand.Parameters.Add("@houseNo", SqlDbType.Int).Value = textBox6.Text;
    adapt.InsertCommand.Parameters.Add("@phoneNumber", SqlDbType.Char).Value = textBox7.Text;
    adapt.InsertCommand.Parameters.Add("@birthDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
    adapt.InsertCommand.Parameters.Add("@startWorkingDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);


    Con.Open();
    adapt.InsertCommand.ExecuteNonQuery();
    Con.Close();
}

如何连接到数据库以便插入?

【问题讨论】:

  • 停止使用 AttachDbFileName。将数据库附加到您的 Express 实例,然后使用逻辑数据库名称而不是指定路径进行连接。
  • 你能告诉我怎么做吗?
  • 嘿,试试谷歌,它是一个很好的搜索引擎!我找到了http://msdn.microsoft.com/en-us/library/ms190209(v=sql.105).aspx
  • Ozren Tkalčec Krznarić,我不需要附加到 sql server,我需要附加到 c#
  • 您混淆了两种不同类型的“附加”...

标签: c# sql .net sql-server database


【解决方案1】:

你需要转义\targil3.mdf

例如,在分配字符串之前使用\\@

SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\targil3.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

【讨论】:

  • @user2023203 - 这听起来像是一个新问题,您必须提出一个新问题,说明您在尝试更新/插入数据库时​​遇到的错误。
  • @user2023203 您如何验证这一点?回到我的第一条评论,使用此 AttachDbFileName 设置的一个问题是,如果您有两个应用程序副本,或者您的应用程序和 Visual Studio 或 Management Studio 都使用相同的连接字符串,那么它们实际上都是在创建他们自己的数据库副本。因此,当您在一个应用程序中运行更新时,另一个应用程序不会看到这些更改。所以,再次,停止使用这个“功能”。正确附加数据库。
【解决方案2】:

这是因为您的连接字符串中有需要转义的字符。为了克服这个问题,你有几个选择。

要么使用

显式转义这些字符
\\

在赋值前使用@符号。

PS:虽然我建议您在 app.config 中指定连接字符串并像这样阅读它

string conString = System.Configuration.ConfigurationManager.ConnectionStrings["yourConnectionString"].ToString();

你应该考虑

的用户
using (SqlConnection sqlConn = new SqlConnection(conString ))
{
   try
   {
        //your sql statements here
    }
   catch (InvalidOperationException)
    {

    }
    catch (SqlException)
    {

    }
    catch (ArgumentException)
    {

    }
 }

您不会遇到上述任何错误。

【讨论】:

  • 我同意在 app.config 中定义它,这样如果它由于某种原因发生变化,您只需在代码中更改它的一个地方,而不是它被调用的每个地方。
猜你喜欢
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
相关资源
最近更新 更多