OleDB connection string for SQL Server in a C# program

OleDB connection string for SQL Server in a C# program

This works as expected on my side. From the error message I strongly suspect that you are using the SqlConnection class instead of the OleDbConnection (Of course you need to use all the other classes provided by OleDb like OleDbCommand, OleDbDataReader etc…)

  string connStr = Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI;
  using(OleDbConnection cnn = new OleDbConnection(connStr))
  {
     ....
  }

When in doubt, use the string builder in visual studio. That way unsupported keywords cant creep into your connection strings, the following is a wonderful example on how to use it.

http://www.c-sharpcorner.com/uploadfile/suthish_nair/how-to-generate-or-find-connection-string-from-visual-studio/

OleDB connection string for SQL Server in a C# program

The same Connection string is working fine at my end.
I am posting my sample code which is executes successfully at my end

public   string connStr = Provider=SQLOLEDB;Data Source=.;Initial  Catalog=<dbName>;Integrated Security=SSPI;
  public OleDbConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {

        Test();
    }

    public void Test()
    {
        con = new OleDbConnection(connStr);
        con.Open();
        OleDbCommand cmd = new OleDbCommand(select * from tblApartments, con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
    }

Please place breakpoint and check line to line and when your breakpoint comes to con.close(); then check ds, you can see the output.

Leave a Reply

Your email address will not be published.