2011-05-24 66 views
0

我已經在我的類文件中編寫了這段代碼來連接到SQL Server 2000,但它不起作用,請幫助我!我的Winforms應用程序沒有連接到SQL Server 2000?

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data.OleDb; 
using System.Data;//to get dataset 
using MySql.Data.MySqlClient; 

namespace CottonPurchase 
{ 
    class Library 
    { 
     private OleDbConnection conn;//provides a connection string 
     //that helps to connect to any database 
     //command: helps to perform INSER/UPDATE/DELETE and SELECT 
     //statements 
     private OleDbCommand cmd; 
     //is used to generate SQL statements that helps you to 
     //bind them to controls such as dta grid view control. 
     private OleDbDataAdapter da; 
     //DataSet helps you to store data retrieved from the database 
     //temporarily so that you dont require to remain connected 
     //with the database once the data is retrieved and stored 
     //into the dataset. 
     private DataSet ds; 

     //Data Reader is used to fetch records from a table 
     //using the command object. 
     private OleDbDataReader dr; 

     /** 
     * Function: GetConnection 
     * Parameter: Nil 
     * ReturnType: bool 
     * Description: This function returns true 
     * if the connection succeeds and false if it fails. 
     * */ 
     public bool GetConnection() 
     { 
      bool flag = false; 
      try 
      { 
       conn = new OleDbConnection("Provider=SQLOLEDB; Data Source=TANYA-PC; User ID=sa; Password=; Database=biore1"); 
       conn.Open();//it opens the connection to the database 
       flag = true; 
      } 
      catch (Exception ex) 
      { 
       flag = false; 
      } 

      return flag; 
     } 

    /** 
    * Function Name: CloseConnection 
    * Return Type: bool 
    * Parameter: Nil 
    * Description: This function closes any active connection 
    * */ 
    public bool CloseConnection() 
    { 
     bool flag = false; 

     try 
     { 
      conn.Close();//This statement closes the connection 
      flag = true; 
     } 
     catch (Exception ex) 
     { 
      flag = false; 
     } 

     return flag; 
    } 

    /** 
    * Function Name: ExecuteSQLStatement 
    * Parameter: string 
    * Return Type: bool 
    * Description: This function passes the INSERT/UPDATE or 
    * DELETE statement as a string parameter and returns true 
    * if the statement succeeds and false if it fails. 
    * */ 
    public bool ExecuteSQLStatement(string sql) 
    { 
     bool flag = false; 
     try 
     { 
      //Command object is used to isert/update or delete 
      cmd = new OleDbCommand(sql, conn); 
      //ExecuteNonQuery function is used for 
      //INSERT/UPDATE and DELETE only 
      cmd.ExecuteNonQuery();//Performs the insertion/deletion or updation 
      flag = true; 
     } 
     catch (Exception ex) 
     { 
      flag = false; 
     } 
     return flag; 
    } 

    /** 
    * Function Name: GenerateID 
    * Parameter: string 
    * Return Type: int 
    * Description: This function passes the table name as parameter 
    * and returns a unique ID as a numeric value. 
    * */ 
    public int GenerateID(string tablename) 
    { 
     int recordcount = 0; 
     try 
     { 
      cmd = new OleDbCommand("SELECT count(*) from " + tablename, conn); 
      // 
      dr = cmd.ExecuteReader();//will fetch the result from the provided query and assign it to the data reader object 
      dr.Read();//will move the record pointer to the first record 

      recordcount = dr.GetInt32(0); 

      recordcount++; 
     } 
     catch (Exception ex) 
     { 
     } 
     return recordcount; 
    } 
} 

} 

問題是我正在使用Windows身份驗證登錄到我的SQL Server?

+2

**什麼錯誤訊息,你看見了什麼?**請編輯您的問題,包括此信息。請編輯爲僅包含與您的錯誤相關的代碼行。 – 2011-05-24 16:02:29

+1

如果您使用Windows身份驗證,那麼爲什麼您的連接字符串指定用戶名和密碼?你應該使用其中一種,而不是兩種。請花時間格式化您的代碼,以便我們閱讀它。最後,你不會說出你得到的錯誤,如果有的話。 – Jason 2011-05-24 16:03:42

+0

如果你連接到SQL Server 2000 - 你爲什麼在文件的頂部有'使用MySql.Data.MySqlClient;'?你有什麼理由使用'OleDbConnection'而不是更適合SQL Server的'SqlConnection'? – 2011-05-24 16:22:39

回答

2

您沒有使用Windows驗證登錄根據您的連接字符串你使用SQL身份驗證嘗試登錄:

conn = new OleDbConnection(
"Provider=SQLOLEDB; Data Source=TANYA-PC; User ID=sa; Password=; Database=biore1"); 

我可以告訴大家,因爲有一個用戶ID和密碼在連接字符串中,而不是受信任的,SSPI或其他參數(特定於提供者等),這些參數需要用可信憑證來形成正確的連接字符串。對於連接字符串幫助,請查看ConnectionStrings.com,然後爲其添加書籤。

(我不知道你是否已經意識到,但密碼字段爲空,至少在示例代碼。)

+0

heyy thaanx但我想通了! – tanya 2011-05-25 19:10:12

+0

@tanya:很好。你介意分享解決方案(你可以回答並接受你自己的問題),以便其他人可以找到它並利用它?謝謝! – 2011-05-25 19:17:44

+0

嘿,你可以請幫我我不知道如何使用網格視圖中的WinForms?請幫忙 – tanya 2011-06-03 18:51:30

相關問題