計數數據存入Mysql

引用dllmysql

MySql.Data.dllsql

建一個數據鏈接靜態類數據庫

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

 

//定義測試字段spa

double countpass;
double countfail;server

 

讀取數據庫數量方法  ip

  getcount();get

private void getcount()
{string


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);


}io

//存入數據

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

相關文章
相關標籤/搜索