1

次のエラーが発生します「無効な認証仕様、無効な接続文字列属性」

 //namespaces
  using System;
  using System.Collections.Generic;
   using System.ComponentModel;
  using System.Data;
 using System.Drawing;
  using System.Linq;
  using System.Text;
 using System.Windows.Forms;
 using System.Data.SqlClient;
 using System.Configuration;
 using System.IO;
 using System.Data.OleDb;

  namespace Database1
 {
   public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    public bool IsValidConnectionForPrinting()
    {
      //string declaration  
        string str = @" Provider = SQLOLEDB; Data Source = sekhar; Database = DMS; UserId = sa; Password = 123";


         //Oledbconnection to database           
        OleDbConnection oleDbcon = new OleDbConnection(str);

         try
        {
            oleDbcon.Open();
            MessageBox.Show("hai");
            oleDbcon.Close();
        }
       //Exception 
     catch (Exception ex) 

        {
            if (ex.Message.StartsWith("Invalid object name"))
            {
                MessageBox.Show(ex.Message.Replace("Invalid object name", "Table or view not found"), "Connection Test");
            }
//Connection 
  private void btnConnTest_Click(object sender, EventArgs e)
    {
        if (IsValidConnectionForPrinting())
        {
            MessageBox.Show("Connection succeeded", "Connection Test");
        }
       }
        }
    }
4

2 に答える 2

0

Databaseorを含まない接続文字列を使用してInitial Catalogから、次のように呼び出すことをお勧めします。

oleDbcon.ChangeDatabase("DMS");

これは、既に経験したように、データベース ドライバーが異なれば、接続文字列でデータベースを参照するための構文も異なるためです。

于 2015-11-12T12:22:57.847 に答える