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

发布于 2024-11-09 14:25:13 字数 3888 浏览 0 评论 0原文

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

I have written this code in my class file to connect to SQL Server 2000 but it doesn't work please help me!!!

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;
    }
}

}

The thing is that I'm using Windows authentication to log into my SQL Server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-11-16 14:25:13

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

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

我可以告诉您,因为连接字符串中有用户 ID 和密码,而不是受信任的、SSPI 或其他使用可信凭据制定正确的连接字符串所需的参数(特定于提供程序等)。有关连接字符串帮助,请查看 ConnectionStrings.com ,然后将其添加为书签。

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

You're not using windows auth to log in. According to your connection string you're using SQL Authentication to try and log in:

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

I can tell because there is a user id and password in the connection string instead of trusted, SSPI or other arguments (specific to provider etc.) necessary to formulate a proper connection string with trusted credentials.For connection string help please take a look at ConnectionStrings.com, and then bookmark it.

(i'm not sure if you're aware but the password field is blank, at least in the example code.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文