【问题标题】:username , password and other parameters in sqlserver connection stringsqlserver 连接字符串中的用户名、密码和其他参数
【发布时间】:2012-08-27 13:39:51
【问题描述】:

我是 C# 和 .net 的新手,刚接触过 php 和 mysql。
要使用 c# 连接到 sqlserver express(在 Visual Studio 2010 中),我们应该提供一个连接字符串,该字符串有很多我在网上找到的格式,特别是在 connectionstrings
例如标准格式:
"数据源=myServerAddress;初始目录=myDataBase;UserId=myUsername;Password=myPassword;"

什么是用户名和密码?我在哪里可以找到它们?它们是像 php mysql 还是其他东西中的“root”和“”?

如前所述,我在 Visual Studio 2012 的 sqlserver express 中创建了一个名为“db.sdf”的数据库。

我真的很困惑。请帮忙。

【问题讨论】:

  • sdf 是一个 SQL Server Compact Edition 数据库。

标签: c# .net sql-server-ce connection-string sql-server-express


【解决方案1】:

如果您的数据库扩展名是SDF,则您正在创建SQL Server CE精简版)数据库文件。使用这个连接字符串

Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf;Persist Security Info=False;

Data Source=MyData.sdf;Persist Security Info=False;

More on this link.

更新 1

您需要System.Data.SqlServerCe 命名空间。

SqlCeConnection conn = new SqlCeConnection("Data Source=\\Mobile\\Northwind.sdf;");
conn.Open();
.
.
.
conn.Close();

来自MSDN
命名空间: System.Data.SqlServerCe
程序集: System.Data.SqlServerCe (在 system.data.sqlserverce.dll)

【讨论】:

  • 不工作。当我添加 con.Open() 时,其余代码不执行,但也没有错误。我不知道为什么
  • 你能发布你的代码吗?也许你使用了错误的数据提供者。
  • 使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Data.SqlClient; namespace ConsoleApplication1 { class First { static void Main() { //string connectionString = ConsoleApplication1.Properties.Settings.Default.ConnectionString; SqlConnection con = new SqlConnection("Data Source=db.sdf;Persist Security Info=False;"); // 2. 打开连接 con.Open(); Console.WriteLine("ok"); Console.ReadLine(); } } }
  • 您需要包含 System.Data.SqlServerCe 命名空间(不是 System.Data.SqlClient)。它在System.Data.SqlServerCe.dll
  • 这个link 提供了一个很棒的教程来创建使用sqlce 数据库文件的应用程序。它甚至会教你如何添加你需要的程序集文件:)
猜你喜欢
  • 1970-01-01
  • 2012-03-29
  • 2011-04-15
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
相关资源
最近更新 更多