目录
  • C#数据库连接(类的形式)
  • C#连接数据库的步骤和相关的方法调用
    • 相关的方法调用
  • 总结

    C#数据库连接(类的形式)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    namespace systemprotect
    {
        class DataCon
        {
            static string strCon = "server=.;database =自己的数据库名;uid=sa;pwd=自己的数据库密码";//数据库连接串
            SqlConnection conn = new SqlConnection(strCon);
            public SqlDataReader query(string str)//查询
            {
                if (this.conn.State == System.Data.ConnectionState.Closed)//判断连接是否打开
                {
                    this.conn.Open();
                }
                SqlCommand cmd = new SqlCommand(str, conn);
                return cmd.ExecuteReader();
            }
            public int insert(string str)//插入,删除,更新 返回影响的行数
            {
                if (this.conn.State == System.Data.ConnectionState.Closed)
                {
                    this.conn.Open();
                }
                SqlCommand cmd = new SqlCommand(str, conn);
                return cmd.ExecuteNonQuery();
            }
            public void close()//关闭连接
            {
                conn.Close();
            }
        }
    }

    C#连接数据库的步骤和相关的方法调用

    //第一步:创建Connection 数据库连接对象
                SqlConnection conn = new SqlConnection("server = . ; uid = sa ; pwd = jnos;database = JINGDONGDB");
                //第二步:打开连接数据库
                conn.Open();
                //第三步:使用数据库
                string sql = $@"select ProductNo, ProductName, ProductImage, 
                                Price,password from Product where ProductNo={_ProductNo}and password='{_password}'";//@符号表示可以换行,代码也连接在一起
                SqlCommand command = new SqlCommand(sql,conn);
                SqlDataReader reader = command.ExecuteReader();//
                if (reader.Read())
                {
                    string ProductNo = reader["ProductNo"].ToString();
                    string ProductName = reader["ProductName"].ToString();
                    MessageBox.Show($"欢迎{ProductName}登录成功");
                }//在数据库里面是为  next() 有数据为 true 没有数据为 flase
                else
                {
                    MessageBox.Show("账号或密码错误,请重新输入!");
                }
                //int resule = command.ExecuteNonQuery();//添加、删除、修改(返回行数受影响)接SqlCommand command = new SqlCommand(sql,conn);
                                                                        //行 是添加删除修改的步骤
                //object result = command.ExecuteScalar();//查询聚合函数用到
                //if(resule > 0)
                //{
                //    MessageBox.Show("删除成功");
                //}
                //else
                //{
                //    MessageBox.Show("删除失败");
                //}
                //第四步:关闭连接对象
                conn.Close();

    相关的方法调用

    • command.ExecuteNonQuery:添加、删除、修改(返回行数受影响
    • command.ExecuteScalar:查询聚合函数
    • command.ExecuteReader:查询单列函数

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。