计数数据存入Mysql

引用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);
}
}

 

转载于:https://www.cnblogs.com/ouyangkai/p/10916761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值