Sql Server Error: No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).

本文解决了一个从Visual Studio 2005使用Access数据库迁移到SQL Server Express时遇到的连接字符串问题。作者最初尝试使用OLEDB进行连接但失败了,最终通过采用SqlConnection类成功连接。

Connection string to SQL server Express

I am having trouble with connection strings from VS 2005 to an SQL Express database.

I have an application now running using an Access Database and OLEDB.    I wanted to convert my data from Access to SQL Server.   All Queries are handled by a module that I wrote that accepts an SQL statement and returns a DataTable.   I would like to use this same technique with an SQL Server database
This is the code from the module 
 

1None.gifconn = New System.Data.OleDb.OleDbConnection
2None.gif  conn.ConnectionString = connectionString
3None.gif  conn.Open()
4None.gif  myTrans = conn.BeginTransaction()
5None.gif  myCommand = New System.Data.OleDb.OleDbCommand(query, conn, myTrans)
6None.gif  myReader = myCommand.ExecuteReader()
7None.gif  -- code not shown creates a datatable from the myReader return 
8None.gif


To see if this would work I used the northwind database.   I added it to my Project and I was able to retrieve data from the table using a table adapter.   I built the table adapter by following the tutorials and was able to populate tables and fill a GridView Control.

The connect string shown in the table adapter was:

 

1None.gif  Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True

 

I tried to connect using this and got the following error:

'Error message -- "An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'." String

I then tried the connection string with the provider information 

1None.gifconnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True;Provider=SQLOLEDB"

And got the following error message

'"No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21)." String

I also tried to give a full path to the database file and failed with the same error message:

 

1None.gif'connectionString = 
2None.gif"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Cassini\NorthWind\App_Data\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True;Provider=SQLOLEDB" 

 

'"No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21)." String

I have looked through this forum and tried many other combinations and can't get it to work.   Any Ideas of what the string should be.

Thanks.



The Soulation Is :

If you are connecting to sql you should be using the sqlclient class instead of OleDb.  Try it like this:

1None.gifdim conn as New System.Data.SqlClient.SqlConnection
2None.gifconn.ConnectionString = connectionString
3None.gifconn.Open()
4None.gifmyTrans = conn.BeginTransaction()
5None.gifmyCommand = New System.Data.SqlClient.sqlcommand(query, conn, myTrans)myReader = myCommand.ExecuteReader()

转载于:https://www.cnblogs.com/zwwon/archive/2007/01/04/611679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值