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

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 
 

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

 

1 None.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 

1 None.gif connectionString  =   " 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:

 

1 None.gif ' connectionString = 
2 None.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:

1 None.gif dim conn  as  New System.Data.SqlClient.SqlConnection
2 None.gifconn.ConnectionString  =  connectionString
3 None.gifconn.Open()
4 None.gifmyTrans  =  conn.BeginTransaction()
5 None.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、付费专栏及课程。

余额充值