MS Access 连接字符串问题 C#

发布于 2024-10-10 09:56:23 字数 1537 浏览 0 评论 0原文

我有以下代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-10-17 09:56:23

有两件事:

  1. 您使用了错误的连接对象; SqlConnection 用于在您尝试与 MS Access 数据库通信时与 SQL Server 数据库通信。尝试使用 OleDbConnection 代替。这也意味着您应该使用 OleDbDataAdapter 而不是 SqlDataAdapter
  2. 该字符串看起来有点奇怪,带有额外的引号和空格。

试试这个:

mycon=new OleDbConnection();
mycon.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
// and then the rest of your code

作为旁注,connectionstrings.com 是此类信息的一个很好的资源(有一个页面对于访问连接字符串)。

Two things:

  1. You are using the wrong connection object; SqlConnection is for communicating with SQL Server databases, while you are trying to talk to an MS Access database. Try using an OleDbConnection instead. This also means that you should use an OleDbDataAdapter instead of an SqlDataAdapter.
  2. The string looks a bit odd with extra quotation marks and spaces.

Try this instead:

mycon=new OleDbConnection();
mycon.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
// and then the rest of your code

As a side note, connectionstrings.com is a great resource for this kind of info (there is a page for Access connectionstrings).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文