private void button1_Click(object sender, EventArgs e)
{
if(txtNum.Text!=""||txtPew.Text!=""||txtModel.Text!=""||txtType.Text!="")
{
int returnValue =0;
string carNum = txtNum.Text.ToString();
string pew = txtPew.Text.ToString();
string model = txtModel.Text.ToString();
string type = txtType.Text.ToString();
DataOp dop=new DataOp();
Boolean bl = dop.IsExits(carNum);
if (bl != true)
{
returnValue = dop.addCar(carNum, pew, model, type);
if(returnValue!=0)
{
MessageBox.Show("信息添加成功!!");
}
else{
MessageBox.Show("数据库出错!!");
}
}
else
{
MessageBox.Show("车辆已存在!!");
txtNum.Text = "";
} }
else
{
MessageBox.Show("信息必须完整!!!");
}***********
public int addCar(string carNum,string pew,string model,string type)
{
int returnValue=0;
OleDbConnection conn = new OleDbConnection(strCon);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "insert into car(carNum,pew,model,type) values('"+carNum+"','"+pew+"','"+model+"','"+type+"')";
try
{
OleDbCommand comm=new OleDbCommand(sql,conn);
returnValue = comm.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show("数据库错误: "+ex.Message);
}
return returnValue;
}
**************
public Boolean IsExits(string carNum)
{
Boolean bl=false;
OleDbConnection conn = new OleDbConnection(strCon);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "select * from car where carNum='" + carNum + "'";
OleDbDataReader oda = null;
try
{
OleDbCommand odc = new OleDbCommand(sql, conn);
oda = odc.ExecuteReader();
if(oda.Read())
{
bl = true;
}
oda.Close(); }
catch (Exception ex)
{
MessageBox.Show("数据库出错: " + ex.Message);
}
return bl;
}
{
if(txtNum.Text!=""||txtPew.Text!=""||txtModel.Text!=""||txtType.Text!="")
{
int returnValue =0;
string carNum = txtNum.Text.ToString();
string pew = txtPew.Text.ToString();
string model = txtModel.Text.ToString();
string type = txtType.Text.ToString();
DataOp dop=new DataOp();
Boolean bl = dop.IsExits(carNum);
if (bl != true)
{
returnValue = dop.addCar(carNum, pew, model, type);
if(returnValue!=0)
{
MessageBox.Show("信息添加成功!!");
}
else{
MessageBox.Show("数据库出错!!");
}
}
else
{
MessageBox.Show("车辆已存在!!");
txtNum.Text = "";
} }
else
{
MessageBox.Show("信息必须完整!!!");
}***********
public int addCar(string carNum,string pew,string model,string type)
{
int returnValue=0;
OleDbConnection conn = new OleDbConnection(strCon);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "insert into car(carNum,pew,model,type) values('"+carNum+"','"+pew+"','"+model+"','"+type+"')";
try
{
OleDbCommand comm=new OleDbCommand(sql,conn);
returnValue = comm.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show("数据库错误: "+ex.Message);
}
return returnValue;
}
**************
public Boolean IsExits(string carNum)
{
Boolean bl=false;
OleDbConnection conn = new OleDbConnection(strCon);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "select * from car where carNum='" + carNum + "'";
OleDbDataReader oda = null;
try
{
OleDbCommand odc = new OleDbCommand(sql, conn);
oda = odc.ExecuteReader();
if(oda.Read())
{
bl = true;
}
oda.Close(); }
catch (Exception ex)
{
MessageBox.Show("数据库出错: " + ex.Message);
}
return bl;
}
我的carNum字段没有设为主键。。是不是这个缘故请高手帮帮忙