为 Localhost c# 使用 Oledb 连接字符串
Using Oledb connection string for Localhost c#
使用Oledb c#连接时
我注意到很多连接使用文件。
但是如何使用 oledb 连接到本地主机?
我使用连接 SQL Express 并使用 window 身份验证
的 Microsoft SQL 服务器管理创建了数据库和表
使用这个函数时,我不知道应该如何转换以连接到本地主机
//Want the connString to connect localhost instead of file
public static string connString = @"Provider=Microsoft.JET.OLEDB.4.0;data source=" + Path + "\database\errDB.mdb";
public static OleDbConnection connection;
public myFunction()
{
string sqlString = "SELECT name,contact,accessLevel,Crudential_ID FROM errors where Crudential_ID =@ID";
connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand(sqlString, connection);
//Open connection
connection.Open();
command.Parameters.Add("@ID", OleDbType.VarChar);
command.Parameters["@ID"].Value = "test";
//Read from database
OleDbDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
.....
}
connection.Close();
}
如果您使用 SQL Express,那么我建议您使用 System.Data.SqlClient.SqlConnection
对象来建立连接。您只需要您的服务器名称即可连接。
Server=ServerName\SQLEXPRESS;Database=Blah;User ID=user;Password=pw
connectionstrings.com - true to its name - is indispensable when you frequently need to construct connection strings. For your specific case, this would be the relevant section。
基于此,您的连接字符串应如下所示:
Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=SOMEDATABASE;Trusted_Connection=yes;
分解:
SQLNCLI11
是 SQL Native Client OLEDB 提供程序。您可以在 SQL Management Studio 中的 Server Objects > Linked Servers > Providers. 下查看可用的提供程序
.\SQLEXPRESS
是您的服务器名称和实例。 .
是 shorthand for localhost
(如果你愿意也可以使用 localhost
),SQLEXPRESS
是 SQL 的默认实例名称Express 安装下。
SOMEDATABASE
- 无论您的数据库名称是什么。
Trusted_Connection=yes
- 使用 windows 身份验证。有时您将其视为 Integrated Security=SSPI
。他们是一回事。
使用Oledb c#连接时 我注意到很多连接使用文件。 但是如何使用 oledb 连接到本地主机? 我使用连接 SQL Express 并使用 window 身份验证
的 Microsoft SQL 服务器管理创建了数据库和表使用这个函数时,我不知道应该如何转换以连接到本地主机
//Want the connString to connect localhost instead of file
public static string connString = @"Provider=Microsoft.JET.OLEDB.4.0;data source=" + Path + "\database\errDB.mdb";
public static OleDbConnection connection;
public myFunction()
{
string sqlString = "SELECT name,contact,accessLevel,Crudential_ID FROM errors where Crudential_ID =@ID";
connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand(sqlString, connection);
//Open connection
connection.Open();
command.Parameters.Add("@ID", OleDbType.VarChar);
command.Parameters["@ID"].Value = "test";
//Read from database
OleDbDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
.....
}
connection.Close();
}
如果您使用 SQL Express,那么我建议您使用 System.Data.SqlClient.SqlConnection
对象来建立连接。您只需要您的服务器名称即可连接。
Server=ServerName\SQLEXPRESS;Database=Blah;User ID=user;Password=pw
connectionstrings.com - true to its name - is indispensable when you frequently need to construct connection strings. For your specific case, this would be the relevant section。
基于此,您的连接字符串应如下所示:
Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=SOMEDATABASE;Trusted_Connection=yes;
分解:
SQLNCLI11
是 SQL Native Client OLEDB 提供程序。您可以在 SQL Management Studio 中的 Server Objects > Linked Servers > Providers. 下查看可用的提供程序
.\SQLEXPRESS
是您的服务器名称和实例。.
是 shorthand forlocalhost
(如果你愿意也可以使用localhost
),SQLEXPRESS
是 SQL 的默认实例名称Express 安装下。SOMEDATABASE
- 无论您的数据库名称是什么。Trusted_Connection=yes
- 使用 windows 身份验证。有时您将其视为Integrated Security=SSPI
。他们是一回事。