2012-04-10 17 views
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"); 
     } 
     } 
     } 
    } 
+0

エラーにはどの行が表示されますか?多分接続文字列が悪いですか? – gideon

+0

oleDbcon.Open(); 接続が開こうとしたときにエラーが発生しました – user1312412

+2

'Database'の代わりに' Initial Catalog'が必要です – V4Vendetta

答えて

0

を取得しています私はDatabaseまたはInitial Catalogが含まれていない接続文字列を使用することをお勧めします、と

oleDbcon.ChangeDatabase("DMS"); 
すでに経験してきたように、異なるデータベースドライバは、STの接続にデータベースを参照するための別の構文を使用し、これは

:その後のようなものを呼び出しますリング。

関連する問題