查询数据库一直提示错误,实在看不出来了。望大家不吝赐教,多谢!!!
private void Form1_Load(object sender, System.EventArgs e)
{
String[] names = new string[4]{ "填海 ", "造船 ", "渔场 ", "捕捞 "};
Int32[] counts = new Int32[4]{0,0,0,0};
string dataBasePath = @ "C:\Documents and Settings\Administrator\My Documents\Demo\database\marine.mdb ";
System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+dataBasePath);
myConn.Open();
if(myConn==null)
{MessageBox.Show( "no connecton ");}
for(int i=1;i <=4;i++)
{
string strSQL = "SELECT COUNT(*) FROM table1 WHERE table1.type= "+names[i-1].ToString();
try
{
System.Data.OleDb.OleDbCommand cmdData = new System.Data.OleDb.OleDbCommand(strSQL,myConn);
counts[i] = (Int32)cmdData.ExecuteScalar();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MessageBox.Show(counts[0].ToString()+counts[1].ToString()+counts[2].ToString()+counts[3].ToString());
}
一直有错误提示:至少有一个参数没有被指定值。
每次都会提示4次。应该是
System.Data.OleDb.OleDbCommand cmdData = new System.Data.OleDb.OleDbCommand(strSQL,myConn);
counts[i] = (Int32)cmdData.ExecuteScalar();这块出的错误吧,但实在找不到,多谢各位帮忙了。。。。。
------解决方案--------------------string strSQL = "SELECT COUNT(*) FROM table1 WHERE table1.type= ' "+names[i-1].ToString()+ " ' ";
------解决方案--------------------楼上的对了
SQL语句``
如果属性为数字类```可以不用引号
如果为字符类````就要用引号
------解决方案--------------------counts[i] = (Int32)cmdData.ExecuteScalar();
也应该改成
counts[i - 1] = (Int32)cmdData.ExecuteScalar();不然counts[0]是不会有值的