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

解决方案 »

  1.   

    结果显示添加成功。。可是access数据库中并没有添加的数据。。很奇怪。。
    我的carNum字段没有设为主键。。是不是这个缘故请高手帮帮忙
      

  2.   

    access数据库中就是四个数据段。。
      

  3.   

    问下各位。。有没可能是access数据库的问题。。不过我手动添加数据是可以的。。
      

  4.   

    问题解决了。。在Visual studio debug下运行 它的数据库是存放在debug数据库下面的。。