我的Winforms应用程序无法连接到SQL Server 2000?

时间:2011-05-24 15:57:19

标签: winforms sql-server-2000

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

1 个答案:

答案 0 :(得分:2)

您没有使用Windows身份验证登录。根据您的连接字符串,您正在使用SQL身份验证尝试登录:

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

我可以告诉,因为连接字符串中有一个用户ID和密码,而不是受信任,SSPI或其他参数(特定于提供者等),需要用可信凭证来形成正确的连接字符串。对于连接字符串帮助,请采取查看ConnectionStrings.com,然后将其加入书签。

(我不确定你是否知道,但密码字段是空白的,至少在示例代码中是这样。)