SQL Server的提供程序未在本地计算机上注册 [英] Provider for SQL Server is not Register On Local Machine
问题描述
我使用以下代码将C#与数据库连接
I used the following code to connect C# with database
string connStr = "Provider=.NET Framework Data Provider for SQL Server"+
" Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True";
try
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
using (OleDbCommand cmd = new OleDbCommand(selected_querry, conn))
{
cmd.CommandType = CommandType.Text;
using (OleDbDataAdapter dAdapter = new OleDbDataAdapter(selected_querry, conn))
{
DataSet ds = new DataSet();
try
{
dAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception)
{
MessageBox.Show("Error: Failed to Fill the required data Field from the DataBase.");
return;
}
}
}
}
else
MessageBox.Show("Connection Not Open");
}
}
catch (Exception e)
{
MessageBox.Show("Error: Failed to retrieve the required data from the DataBase."+e);
return;
}
当我运行代码时,我总是会遇到以下错误
When i run the code i always get the following error
Provider for SQL Server is not Register On Local Machine
推荐答案
对于OleDb连接,您需要在连接字符串中指定OLE DB提供程序而不是.NET提供程序,并指定OLE DB连接字符串关键字Server (而不是数据源)和Trusted_Connection =是(而不是Integrated Security = SSPI):
With an OleDb connection, you need to specify an OLE DB provider rather than a .NET provider in the connection string and specify the OLE DB connection string keywords Server (instead of Data Source) and Trusted_Connection=yes (instead of Integrated Security=SSPI):
string connStr = "Provider=SQLNCLI11.1"+
";Server=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\\Database1.mdf;Trusted_Connection=Yes";
SQL Server OLE DB提供程序包括:
SQL Server OLE DB providers include:
- SQLOLEDB:旧版MDAC / WDAC提供程序
- SQLNCLI:SQL Server 2005本机客户端
- SQLNCLI10:SQL Server 2008本机客户端
- SQLNCLI11:SQL Server 2012本机客户端
- SQLOLEDB: legacy MDAC/WDAC provider
- SQLNCLI: SQL Server 2005 Native client
- SQLNCLI10: SQL Server 2008 Native client
- SQLNCLI11: SQL Server 2012 Native client
但是,在.NET应用程序中,最好使用托管提供程序。对于SQL Server,用于SQL Server的.NET Framework数据提供程序(又称为SqlClient)将表现更好,尤其是对于大型结果集。使用SqlConnection,SqlCommand等,并从连接字符串中省略提供程序,因为它是隐式的。如果删除提供程序规范,则您的连接字符串应该可以使用。
However, in .NET applciations, it is best to use a managed provider. For SQL Server, the .NET Framework Data Provider for SQL Server (a.k.a SqlClient) will perform better, especially with large result sets. Use SqlConnection, SqlCommand, etc. and omit the provider from the connection string since it is implicit. Your connection string should work if you remove the Provider specification.
这篇关于SQL Server的提供程序未在本地计算机上注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!