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;
                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;
                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;
                Connection.Open();
                MySqlDataReader reader = command.ExecuteReader();
                if (!reader.Read())
                        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>");
                            Response.Write("<script>alert('添加失败')</script>");
                    catch (Exception ex)
                        Response.Write("<script>alert(ex.Message)</script>");
                    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 + "'";
数据库内有很多字段,但是2个字段(id、mac_id)中的任何一个都不能在数据库的记录中重复。 WEB网页上有n个TEXTBOX控件,用于输入数据库字段的值。其中 textbox1输入id字段值 textbox3输入mac_id字段的值。 插入数据库之前,需要在数据库中查询,2个字段(id、mac_id)中的任何一个都不在数据库的记录中查询到时,才能将当前数据插入到数据库。 一、连接... 当操作积分用户表,如果accountId在表中没有数据,那么我们新增一条数据,设置用户积分。如果accountId在表中有数据,我们需要更新用户积分。 这个操作简单来说就是: 在单线程下我们查询后处理当然没有问题,但是在并发下问题就显而易见了,系统里可能同插入两条一样的accountId数据。...
知识点:理解 ASP.NET 的概念和优势、会创建 ASP.NET Web Form 程序、理解 ASP.NET Web Form 程序的执行阶段、会使用代码内嵌和代码后置两种方式进行编码。 1、网站开发基础了解 1.1 网站开发各环节组成 1.2 开发网站需要的知识结构 (1)网站开发前台页面技术 页面设计:HTML、DIV+CSS 页面特效:JavaScript、jQuery (2)OOP编程核心公共技能 C#...
string time = Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm");//24小 string time = Convert.ToDateTime(date).ToString("yyyy-MM-dd hh:mm");//12小 "yyyy-MM-dd HH:mm" --->转换为24小制 "yyyy-MM-dd hh:mm" --->转换为12小制 一、12小制验证
C#中的Datatable数据变量的操作过程中,有候我们需要知道DataTable中是否含有数据行或者DataTable的数据总行数,此我们就可以拿到DataTable中的Rows属性对象,通过Rows属性对象的Count属性即可获取总行数。 例如我们我们有个DataTable变量为dt,需要通过C#获取该DataTable的总行数可使用下列语句: introwCount = dt.Rows.Count; 一、C#获取该DataTable的总行数源程序
要将数据库ASP.NETC# 连接,你需要使用 ADO.NET。ADO.NET 提供了一种创建与 .NET 应用程序交互的数据访问层的方式。以下是连接数据库的步骤: 1. 创建连接字符串:连接字符串是一个包含有关数据库服务器的信息的字符串。例如,如果你使用 Microsoft SQL Server,则连接字符串可能包含服务器名称、数据库名称、用户名和密码等信息。 2. 创建连接对象:使用连接字符串创建一个连接对象。在 C# 中,你可以使用 SqlConnection 类来创建连接对象。例如: SqlConnection connection = new SqlConnection(connectionString); 3. 打开连接:使用 Open 方法打开连接。 connection.Open(); 4. 创建命令对象:使用连接对象创建一个命令对象。命令对象用于执行 SQL 查询和存储过程。 SqlCommand command = new SqlCommand(sql, connection); 5. 执行查询:使用 ExecuteReader 方法执行查询,并获取结果集。 SqlDataReader reader = command.ExecuteReader(); 6. 处理结果集:使用 SqlDataReader 对象处理结果集。 7. 关闭连接:使用 Close 方法关闭连接。 connection.Close(); 以上是连接数据库的基本步骤。你可以根据自己的需求进行更高级的操作