计数数据存入Mysql
阅读原文时间:2023年07月11日阅读:2

引用dll

MySql.Data.dll

建一个数据连接静态类

public static class mysql
{
public static string constr = "database=test;Password=密码;user ID=root;server=ip地址";
public static MySqlConnection conn = new MySqlConnection(constr);
}

//定义测试字段

double countpass;
double countfail;

读取数据库数量方法

getcount();

private void getcount()
{

countfail = 0;
countpass = 0;
mysql.conn.Open();//打开数据库
MySqlCommand cons = new MySqlCommand("select * from testlog where 时间 = '" + System.DateTime.Now.ToString("yyyy-MM-dd") + "'and 机型 = 'xxxx'and 站别='"+ comboBox1 .Text+ "'", mysql.conn);//
MySqlDataReader readers = cons.ExecuteReader();
if (readers.Read())
{
countpass = Convert.ToInt32(readers[1].ToString());
countfail = Convert.ToInt32(readers[2].ToString());
}
else
{
countpass = 0;
countfail = 0;
mysql.conn.Close();
mysql.conn.Open();
string sqlstr = "insert into testlog values('xxxx','" + 0 + "','" + 0 + "','" + 0 + "','" + System.DateTime.Now.ToString("yyyy-MM-dd") + "','" + comboBox1.Text + "')";
MySqlCommand comm = new MySqlCommand(sqlstr, mysql.conn);
comm.ExecuteNonQuery();
countpass = 0;
countfail = 0;
mysql.conn.Close();
}
mysql.conn.Close();
label14.Text = Convert.ToString(countfail + countpass);
label5.Text = Convert.ToString(countpass);
label6.Text = Convert.ToString(countfail);

}

//存入数据

private void count()
{
if (ConsTant.test == 2)
{

}
else
{
mysql.conn.Open();//打开数据库
double Num = countpass + countfail;
string sqlstr = "update testlog set 成功数 = '" + countpass + "', 失败数 = '" + countfail + "', 总数 ='" + Num + "' where 时间='" + System.DateTime.Now.ToString("d") + "' and 站别='" + comboBox1.Text + "'and 机型 = 'xxx'";
MySqlCommand comm = new MySqlCommand(sqlstr, mysql.conn);
int iRet = comm.ExecuteNonQuery();
mysql.conn.Close();
//label14.Text = Convert.ToString(countfail + countpass);
//label5.Text = Convert.ToString(countpass);
//label6.Text = Convert.ToString(countfail);
}
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章