MS Access 连接字符串问题 C#
我有以下代码,
mycon=new SqlConnection();
mycon.ConnectionString="'Provider =Microsoft.ACE.OLEDB.12.0';Data Source='G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb'";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
但出现以下错误。
初始化字符串的格式不符合从索引 0 开始的规范。
您能否指导我正确的连接字符串。
谢谢 编辑
public class dataManipulationClass
{
public OleDbConnection mycon;
public DataSet myds;
public DataTable mytable;
public SqlDataAdapter myadap;
public OleDbCommand mycomm;
public bool ManupulateData()
{
mycon = new OleDbConnection();
mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
mycomm=new OleDbCommand();
mycomm.CommandType=CommandType.Text;
mycomm.CommandText = "SELECT * FROM Abbriviations";
mycomm.Connection=mycon;
myadap.SelectCommand=mycomm;
return true;
}
}
现在我在 mycommm 收到以下错误。
Cannot implicitly convert type 'System.Data.OleDb.OleDbCommand' to 'System.Data.SqlClient.SqlCommand'
谢谢
I have the following code
mycon=new SqlConnection();
mycon.ConnectionString="'Provider =Microsoft.ACE.OLEDB.12.0';Data Source='G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb'";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
I am getting the following error.
Format of the initialization string does not conform to specification starting at index 0.
Can you please guide me the correct connectionstring for this.
Thanks
Edit
public class dataManipulationClass
{
public OleDbConnection mycon;
public DataSet myds;
public DataTable mytable;
public SqlDataAdapter myadap;
public OleDbCommand mycomm;
public bool ManupulateData()
{
mycon = new OleDbConnection();
mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
mycomm=new OleDbCommand();
mycomm.CommandType=CommandType.Text;
mycomm.CommandText = "SELECT * FROM Abbriviations";
mycomm.Connection=mycon;
myadap.SelectCommand=mycomm;
return true;
}
}
Now i am getting the following error at mycommm.
Cannot implicitly convert type 'System.Data.OleDb.OleDbCommand' to 'System.Data.SqlClient.SqlCommand'
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事:
SqlConnection
用于在您尝试与 MS Access 数据库通信时与 SQL Server 数据库通信。尝试使用OleDbConnection
代替。这也意味着您应该使用OleDbDataAdapter
而不是SqlDataAdapter
。试试这个:
作为旁注,connectionstrings.com 是此类信息的一个很好的资源(有一个页面对于访问连接字符串)。
Two things:
SqlConnection
is for communicating with SQL Server databases, while you are trying to talk to an MS Access database. Try using anOleDbConnection
instead. This also means that you should use anOleDbDataAdapter
instead of anSqlDataAdapter
.Try this instead:
As a side note, connectionstrings.com is a great resource for this kind of info (there is a page for Access connectionstrings).