我基本上是编码方面的新手,我是uni,我们有一个小项目要完成,我正在使用OleDb来完成它,就像我以前使用过的那样,我正在尝试计算栏位等于特定值,但我总是得到0的列。更明确地说,我想要统计资料显示完成调查(我正在制作的程序)的男性/女性百分比,目前我有1个男性, 2个女性,所以我所有行的字符串都显示3,这是正确的,但是当我要计算所有包含女性的行时,它显示0,所以总百分比为0,本应显示统计信息的文本框显示0,这是代码:
private void Form2_Load(object sender, EventArgs e)
{
String connectionString;
connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\kubas\Desktop\MyProject\ProjectDB.accdb'";
using (OleDbConnection myConnection = new OleDbConnection(connectionString))
{
string Gender1 = "Female";
OleDbConnection con;
OleDbCommand cmd = new OleDbCommand();
OleDbCommand cmd1 = new OleDbCommand();
con = new OleDbConnection(connectionString);
con.Open();
cmd.CommandText = "SELECT Count(Gender) FROM Survey WHERE Gender = '" + Gender1 + "'";
cmd1.CommandText = "SELECT Count(Gender) FROM Survey";
cmd.Connection = con;
cmd1.Connection = con;
con.Close();
try
{
con.Open();
int total = (Int32)cmd.ExecuteScalar();
int total1 = (Int32)cmd1.ExecuteScalar();
int tot;
tot = total / total1 * 100;
string ttal = Convert.ToString(tot);
textBox1.Text = ttal;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
对不起,如果您无法理解某些内容,英语不是我的母语。
也许尝试此命令查询:"SELECT Count(Gender) FROM Survey WHERE Gender = \"" + Gender1 + "\"";
据我所记得。访问对字符串使用引号。