我的Winforms应用程序没有连接到SQL Server 2000?

问题描述:

我已经在我的类文件中编写了这段代码来连接到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

您没有使用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