C# ASP.NET 连接MySQL 如何先进行多条件查询,然后进行数据库插入操作

27 篇文章 3 订阅

        数据库内有很多字段,但是2个字段(id、mac_id)中的任何一个都不能在数据库的记录中重复。

        WEB网页上有n个TEXTBOX控件,用于输入数据库字段的值。其中

              textbox1输入id字段值

               textbox3输入mac_id字段的值。

     插入数据库之前,需要在数据库中查询,2个字段(id、mac_id)中的任何一个都不在数据库的记录中查询到时,才能将当前数据插入到数据库。

 

   一、连接数据库

            string res_id = DropDownList1.SelectedValue.ToString();
            string res_name = DropDownList1.SelectedItem.Text;
            string id = DropDownList1.SelectedValue.ToString() + TextBox1.Text;
            int timing = Convert.ToInt32(TextBox2.Text);//   int.Parse(TextBox2.Text);
            string mac_id = TextBox3.Text;

            string commandStr = "select *from table1 where id = '" + id + "'";
            commandStr += "or mac_id='" + mac_id + "'";
            string ConnectionStr = "server=localhost;port=3306;user Id=root;password=winelec68331; database=monitorid ;Allow User Variables=True";
            MySqlConnection Connection = new MySqlConnection(ConnectionStr);
            MySqlCommand command = new MySqlCommand();
            command.Connection = Connection;
            command.CommandText = commandStr;
           try
            {
                Connection.Open();
            }

二、查询数据库中既没有id也没有mac_id

          string res_id = DropDownList1.SelectedValue.ToString();
            string res_name = DropDownList1.SelectedItem.Text;
            string id = DropDownList1.SelectedValue.ToString() + TextBox1.Text;
            int timing = Convert.ToInt32(TextBox2.Text);//   int.Parse(TextBox2.Text);
            string mac_id = TextBox3.Text;

            string commandStr = "select *from table1 where id = '" + id + "'";
            commandStr += "or mac_id='" + mac_id + "'";
            string ConnectionStr = "server=localhost;port=3306;user Id=root;password=winelec68331; database=monitorid ;Allow User Variables=True";
            MySqlConnection Connection = new MySqlConnection(ConnectionStr);
            MySqlCommand command = new MySqlCommand();
            command.Connection = Connection;
            command.CommandText = commandStr;
            try
            {
                Connection.Open();
                MySqlDataReader reader = command.ExecuteReader();
                if (!reader.Read())
                {
                }
             }

三、插入数据库

查询到数据库中既没有id也没有mac_id时,插入数据库

          string res_id = DropDownList1.SelectedValue.ToString();
            string res_name = DropDownList1.SelectedItem.Text;
            string id = DropDownList1.SelectedValue.ToString() + TextBox1.Text;
            int timing = Convert.ToInt32(TextBox2.Text);//   int.Parse(TextBox2.Text);
            string mac_id = TextBox3.Text;

            string commandStr = "select *from table1 where id = '" + id + "'";
            commandStr += "or mac_id='" + mac_id + "'";
            string ConnectionStr = "server=localhost;port=3306;user Id=root;password=winelec68331; database=monitorid ;Allow User Variables=True";
            MySqlConnection Connection = new MySqlConnection(ConnectionStr);
            MySqlCommand command = new MySqlCommand();
            command.Connection = Connection;
            command.CommandText = commandStr;
            try
            {
                Connection.Open();
                MySqlDataReader reader = command.ExecuteReader();
                if (!reader.Read())
                {
                    try
                    {
                        if (Connection != null)
                            Connection.Close();//关闭连接
                          
                        Connection.Open();
                        
                        MySqlCommand cmd = new MySqlCommand("insert into table1 set id=@id , res_id=@res_id , res_name=@res_name  , mac_id=@mac_id", Connection);
                        cmd.Parameters.AddWithValue("@id", id);
                        cmd.Parameters.AddWithValue("@res_id", res_id);
                        cmd.Parameters.AddWithValue("@res_name", res_name);
                        cmd.Parameters.AddWithValue("@mac_id", mac_id);
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            Response.Write("<script>alert('添加成功')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('添加失败')</script>");
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert(ex.Message)</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('错误信息:数据库中已经存在id或者mac_id...')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert(ex.Message)</script>");
            }
            finally
            {
                if (Connection != null)
                    Connection.Close();//关闭连接  
            }

总之,在数据库中查询多个条件中任何一个满足,可以使用以下方法:

 string commandStr = "select *from table1 where id = '" + id + "'";
            commandStr += "or mac_id='" + mac_id + "'";
            commandStr += "res_name='" + res_name + "'";
            commandStr += "timing='" + timing + "'";

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值