SoFunction
Updated on 2025-03-07

C# database connection method (class form)

C# database connection (class form)

using System;
using ;
using ;
using ;
using ;
using ;
namespace systemprotect
{
    class DataCon
    {
        static string strCon = "server=.;database =Your own database name;uid=sa;pwd=Your own database password";//Database connection string        SqlConnection conn = new SqlConnection(strCon);
        public SqlDataReader query(string str)//Inquiry        {
            if ( == )//Display whether the connection is open            {
                ();
            }
            SqlCommand cmd = new SqlCommand(str, conn);
            return ();
        }
        public int insert(string str)//Insert, delete, update Return the number of rows affected        {
            if ( == )
            {
                ();
            }
            SqlCommand cmd = new SqlCommand(str, conn);
            return ();
        }
        public void close()//Close the connection        {
            ();
        }
    }
}

Steps and related method calls for C# to connect to the database

//Step 1: Create Connection database connection object            SqlConnection conn = new SqlConnection("server = . ; uid = sa ; pwd = jnos;database = JINGDONGDB");
            //Step 2: Open the connection to the database            ();
            //Step 3: Use the database            string sql = $@"select ProductNo, ProductName, ProductImage, 
                            Price,password from Product where ProductNo={_ProductNo}and password='{_password}'";//@ symbols indicate that lines can be broken, and the codes are also connected together            SqlCommand command = new SqlCommand(sql,conn);
            SqlDataReader reader = ();//
            if (())
            {
                string ProductNo = reader["ProductNo"].ToString();
                string ProductName = reader["ProductName"].ToString();
                ($"welcome{ProductName}Login successfully");
            }//In the database, it is next(). There is data true. No data is flase.            else
            {
                ("The account or password is incorrect, please re-enter!");
            }
            //int resolve = ();//Add, delete, modify (return to the number of rows affected) SqlCommand command = new SqlCommand(sql,conn);                                                                    //Line is the step to add, delete and modify            //object result = ();//Query aggregate function for use            //if(resule > 0)
            //{
            // ("Delete successfully");            //}
            //else
            //{
            // ("Delete failed");            //}
            //Step 4: Close the connection object            ();

Related method calls

  • : Add, delete, modify (return the number of rows is affected
  • : Query the aggregate function
  • : Query single column function

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.