数据库内有很多字段,但是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 + "'";