当前位置:编程学习 > C#/ASP.NET >>

一个C#+access数据库的问题,各位朋友帮帮忙,毕业设计头痛中。。。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
namespace StoreMIS
{
public class InAdd : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
。。。。。。。。。。。。。。。。。。。。。。。
private System.ComponentModel.Container components = null;
                  private OleDbConnection oleConnection1 = null;
                  public DateTimePicker date1;
private OleDbCommand oleCommand1 = null;

public InAdd()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
this.oleCommand1 = new OleDbCommand();
this.oleCommand1.Connection = this.oleConnection1;
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.comMID = new System.Windows.Forms.ComboBox();
            this.textUnit = new System.Windows.Forms.TextBox();
。。。。。。。。。。。。。。。。。。。
            this.label5 = new System.Windows.Forms.Label();
。。。。。。。。。。。。。。。。。。
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.date1 = new System.Windows.Forms.DateTimePicker();
            this.textStore = new System.Windows.Forms.TextBox();
。。。。。。。。。。。。。。。。
            this.textAccount = new System.Windows.Forms.TextBox();
            this.label12 = new System.Windows.Forms.Label();
。。。。。。。。。。。。。。。。。。。。。。。。
            this.textRemark = new System.Windows.Forms.TextBox();
            this.btAdd = new System.Windows.Forms.Button();
            this.btNew = new System.Windows.Forms.Button();
            this.btClose = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.comMID);
。。。。。。。。。。。。。。。
            this.groupBox1.Location = new System.Drawing.Point(8, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(192, 200);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "图书信息";
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.date1);
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
            this.groupBox2.Location = new System.Drawing.Point(208, 8);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(192, 200);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "入库信息";
            // 
            // date1
            // 
            this.date1.Location = new System.Drawing.Point(80, 96);
            this.date1.Name = "date1";
            this.date1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.date1.Size = new System.Drawing.Size(104, 21);
            this.date1.TabIndex = 9;
            this.date1.Value = new System.DateTime(2011, 5, 9);
            this.date1.ValueChanged += new System.EventHandler(this.date1_ValueChanged);
            // 
            // textStore
            // 
            this.textStore.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textStore.Location = new System.Drawing.Point(80, 168);
            this.textStore.Name = "textStore";
            this.textStore.Size = new System.Drawing.Size(104, 21);
            this.textStore.TabIndex = 12;
            // 
            // textSaver
            // 
            this.textSaver.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textSaver.Location = new System.Drawing.Point(80, 144);
            this.textSaver.Name = "textSaver";
            this.textSaver.Size = new System.Drawing.Size(104, 21);
            this.textSaver.TabIndex = 11;
            // 
            // textDealer
            // 
            this.textDealer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textDealer.Location = new System.Drawing.Point(80, 120);
            this.textDealer.Name = "textDealer";
            this.textDealer.Size = new System.Drawing.Size(104, 21);
            this.textDealer.TabIndex = 10;
            // 
            // textValue
            // 
            this.textValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textValue.Location = new System.Drawing.Point(80, 72);
            this.textValue.Name = "textValue";
            this.textValue.Size = new System.Drawing.Size(104, 21);
            this.textValue.TabIndex = 8;
            // 
            // textPrice
            // 
            this.textPrice.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textPrice.Location = new System.Drawing.Point(80, 48);
            this.textPrice.Name = "textPrice";
            this.textPrice.Size = new System.Drawing.Size(104, 21);
            this.textPrice.TabIndex = 7;
            this.textPrice.TextChanged += new System.EventHandler(this.textPrice_TextChanged);
            // 
            // textAccount
            // 
            this.textAccount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textAccount.Location = new System.Drawing.Point(80, 24);
            this.textAccount.Name = "textAccount";
            this.textAccount.Size = new System.Drawing.Size(104, 21);
            this.textAccount.TabIndex = 6;
            this.textAccount.TextChanged += new System.EventHandler(this.textAccount_TextChanged);
            // 
            // btAdd
            // 
            this.btAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btAdd.Location = new System.Drawing.Point(32, 296);
            this.btAdd.Name = "btAdd";
            this.btAdd.Size = new System.Drawing.Size(75, 23);
            this.btAdd.TabIndex = 14;
            this.btAdd.Text = "保存";
            this.btAdd.Click += new System.EventHandler(this.btAdd_Click);
            // InAdd
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.BackColor = System.Drawing.Color.AliceBlue;
            this.ClientSize = new System.Drawing.Size(408, 326);
            this.Controls.Add(this.btClose);
            this.Controls.Add(this.btNew);
            this.Controls.Add(this.btAdd);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "InAdd";
            this.Text = "添加入库信息";
            this.Load += new System.EventHandler(this.InAdd_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.ResumeLayout(false);

}
#endregion
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

private void btClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void btAdd_Click(object sender, System.EventArgs e)
{
            if (comMID.Text.Trim() == "")
MessageBox.Show("请填写图书编号!","提示");
else
{
oleConnection1.Open();
                string sql = "select * from ininfo where MID='" +comMID.Text.Trim()+ "'";
this.oleCommand1.CommandText = sql;

if (null == oleCommand1.ExecuteScalar())
{
string sql1 = "insert into ininfo (MID,InAccount,InPrice,InValue,InDate,InDealer,InSaver,InStore,Remark) values "+
                        "('" +comMID.Text.Trim()+ "','" +textAccount.Text.Trim()+ "'," +
"'"+textPrice.Text.Trim()+"','"+textValue.Text.Trim()+"','"+date1.Text.Trim()+"','"+textDealer.Text.Trim()+"',"+
"'"+textSaver.Text.Trim()+"','"+textStore.Text.Trim()+"','"+textRemark.Text.Trim()+"')";
oleCommand1.CommandText=sql1;
oleCommand1.ExecuteNonQuery();
MessageBox.Show("添加入库信息成功!","提示");
this.Close();

else
MessageBox.Show("图书编号"+comMID.Text.Trim()+"已经存在!","警告");
oleConnection1.Close();
}
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
调试过程中,出现错误,中断,显示为:
未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 System.Data.dll 中。

其他信息: 标准表达式中数据类型不匹配。
逐步调试时,到贴图中倒数第10行的oleCommand1.ExecuteNonQuery()就再也进行不下去了。

当我把sql语句中的'"+date1.Text.Trim()+"'去掉再调试的时候就可以执行,但是我必须有时间潜入这一步。。。

希望高手点拨一下!感激不尽!辛苦毕设中,谢谢各位给予提示!! --------------------编程问答-------------------- string sql1 = "insert into ininfo (MID,InAccount,InPrice,InValue,InDate,InDealer,InSaver,InStore,Remark) values "+
  "('" +comMID.Text.Trim()+ "','" +textAccount.Text.Trim()+ "'," +
"'"+textPrice.Text.Trim()+"','"+textValue.Text.Trim()+"','"+date1.Text.Trim()+"','"+textDealer.Text.Trim()+"',"+
"'"+textSaver.Text.Trim()+"','"+textStore.Text.Trim()+"','"+textRemark.Text.Trim()+"')";
在这句话上设断点。看看生成的sql语句是什么
再就是,看看date1.Text.Trim()的值,是不是空。 --------------------编程问答-------------------- 检查date1.Text的格式,是否是正确的时间格式。 --------------------编程问答-------------------- 看看date1.Text.Trim()的值类型。
这个变量获取的值,应该是转换不成DateTime类型的,如果数据库中,你设置的字段是DateTime类型的话。 --------------------编程问答-------------------- 把sql语句打印出来,或者设置短点,看看对不对,然后作对应修改
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,