C# connection operation database

First I created a database
insert image description hereinsert image description here
about the database information (will be used in the connection string)
insert image description here

1. Connect to the database

1. Download mysql.data.dll: http://soft.onlinedown.net/soft/618668.htm
2. Add reference to Explorer
insert image description here
3. Import namespace

using MySql.Data.MySqlClient;

4. Execute the statement
Use the Connextion object to connect the application to the database

#region 连接数据库
            //定义连接字符串
            string connStr = "Database=register;Data Source=127.0.0.1;port=3306;User Id=root;";
            MySqlConnection conn = new MySqlConnection(connStr);//创建Connection对象
            conn.Open();//打开数据库
#endregion

2. Query data

1. The Command object is a reference to a SQL server command or stored procedure. In addition to retrieving and updating data, the command object can be used to perform some query tasks on the data source that do not return result sets, as well as execute data definition commands that change the structure of the data source
. 2. The DataReader object returns a read-only, forward-only stream of data from the Command. DataReader can only keep one row in memory at a time

#region 查询
            //创建命令
            string sql = "select * from manager";//执行语句
            MySqlCommand cmd = new MySqlCommand(sql, conn);//创建Command对象
            //执行命令--读取数据
            MySqlDataReader reader = cmd.ExecuteReader();//创建MySqlDataReader对象
            while (reader.Read())//每次读一行显示在集合中
            {
    
    
                listBox1.Items.Add(string.Format("编号\t用户名\t密码"));
                listBox1.Items.Add(string.Format("{0}\t{1}\t{2}", reader[0], reader[1],reader[2]));
            }
#endregion

insert image description here
write a login program

 #region 查询
            //创建命令
            string userName = username.Text;//获取输入用户名
            string passWord = password.Text;//获取输入密码
            string sqlSel = "select count(*) from manager where userName = '" + userName + "' and password = '" + passWord + "'";//查询语句
            MySqlCommand com = new MySqlCommand(sqlSel, conn);
            //判断executeScalar方法返回的参数是否大于0,大于0表示查找有数据
            if (Convert.ToInt32(com.ExecuteScalar()) > 0)
            {
    
    
                MessageBox.Show("登录成功!");
            }
            else
            {
    
    
                MessageBox.Show("账户或者密码错误!");
            }
            #endregion

insert image description here

3. Add data

method one:

 #region 插入
            string userName = username.Text;
            string passWord = password.Text;
            string iD = Id.Text;
            MySqlCommand cmd = new MySqlCommand("insert into manager set id=@idd , userName=@un , passWord=@pwd", conn);
            cmd.Parameters.AddWithValue("idd", iD);
            cmd.Parameters.AddWithValue("un", userName);//添加值
            cmd.Parameters.AddWithValue("pwd", passWord);
             //执行语句
            cmd.ExecuteNonQuery();
#endregion

Method Two:

#region 插入
            string userName = username.Text;//获取插入姓名
            string passWord = password.Text;//获取插入密码
            string iD = Id.Text;//获取插入编号
            string sql = "SELECT id,userName,PASSWORD FROM manager";//定义sql语句
            //创建SaqDataAdapter对象
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            //创建SqlCommandBuilder对象
            MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
            //创建DataTable对象
            DataTable dt = new DataTable();
            da.Fill(dt);
            //创建数据行对象
            DataRow row = dt.NewRow();
            //为数据行中每列赋上前台文本框的输入值
            row[0] = iD;
            row[1] = userName;
            row[2] = passWord;
            //将创建的数据行添加到Data Table对象中
            dt.Rows.Add(row);
            //更新数据表
            da.Update(dt);
#endregion

insert image description here

4. Delete data

#region 删除
            MySqlCommand cmd = new MySqlCommand("delete from manager where id=@id", conn);
            string iD = Id.Text;
            cmd.Parameters.AddWithValue("id", iD);
            cmd.ExecuteNonQuery();
#endregion

insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324957915&siteId=291194637