主题:再次请教C#Access数据库编程总提示未处理的类型的异常
首先对11约3日franchdream先生关于《C#Access数据库编程总提示未处理的类型的异常》的解答表示感谢。
经过几天的学习,对C#Access数据库编程子认为有了一定了解,试着做了一段小测试程序。
首先建了一个库名位select2,库中表格为select_2.表中三个字段,第一字段为MYID,第二字段为ROOMTEAM,第三字段为SELECT.
然后在窗体上放置了两个按钮:1。连接数据库按钮button1。2。修改数据库按钮cmdEdit。用数据控件OleDbConnection建了一个连接对象myconnect2.
反复检查CommandText字符串的赋值。没查出错误,怀疑建库过程不对。但由于水平有限,不知错在何处?望高人指点。
源程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace 数据库操作
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
// System.Data.OleDb.OleDbConnection myConnect=new System.Data.OleDb.OleDbConnection();
private System.Windows.Forms.Button cmdEdit;
private System.Data.OleDb.OleDbConnection myconnect2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.cmdEdit = new System.Windows.Forms.Button();
this.myconnect2 = new System.Data.OleDb.OleDbConnection();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 5;
this.button1.Text = "联接数据库";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// cmdEdit
//
this.cmdEdit.Location = new System.Drawing.Point(400, 40);
this.cmdEdit.Name = "cmdEdit";
this.cmdEdit.Size = new System.Drawing.Size(80, 24);
this.cmdEdit.TabIndex = 8;
this.cmdEdit.Text = "修改";
this.cmdEdit.Click += new System.EventHandler(this.cmdEdit_Click);
//
// myconnect2
//
this.myconnect2.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""F:\Select2.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(680, 273);
this.Controls.Add(this.cmdEdit);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "编辑客户数据";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// ///////////连接数据库//////////////////
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
myconnect2.Open();
MessageBox.Show("数据库连接完毕!!");
}
private void cmdEdit_Click(object sender, System.EventArgs e)
{
//string select1="true";
//string id="1";
//int id=1;
/////////////字符串中的变量应用单引号括起来/////////////
/////////////////双引号之间不应进行换行/////////////////
//string strUpdate= "UPDATE selectlab SET "+"SELECT='"+select1+"'"+"WHERE ID=" +id;
//string strUpdate= "UPDATE Select_2 SET SELECT='true' WHERE ID=1";
OleDbCommand myCmd1=myconnect2.CreateCommand();
myCmd1.CommandText="UPDATE Select_2 SET SELECT='ABCD' WHERE MYID=1";
myCmd1.ExecuteNonQuery();
MessageBox.Show("更新数据一笔!!");
}
}
}
经过几天的学习,对C#Access数据库编程子认为有了一定了解,试着做了一段小测试程序。
首先建了一个库名位select2,库中表格为select_2.表中三个字段,第一字段为MYID,第二字段为ROOMTEAM,第三字段为SELECT.
然后在窗体上放置了两个按钮:1。连接数据库按钮button1。2。修改数据库按钮cmdEdit。用数据控件OleDbConnection建了一个连接对象myconnect2.
反复检查CommandText字符串的赋值。没查出错误,怀疑建库过程不对。但由于水平有限,不知错在何处?望高人指点。
源程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace 数据库操作
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
// System.Data.OleDb.OleDbConnection myConnect=new System.Data.OleDb.OleDbConnection();
private System.Windows.Forms.Button cmdEdit;
private System.Data.OleDb.OleDbConnection myconnect2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.cmdEdit = new System.Windows.Forms.Button();
this.myconnect2 = new System.Data.OleDb.OleDbConnection();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 5;
this.button1.Text = "联接数据库";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// cmdEdit
//
this.cmdEdit.Location = new System.Drawing.Point(400, 40);
this.cmdEdit.Name = "cmdEdit";
this.cmdEdit.Size = new System.Drawing.Size(80, 24);
this.cmdEdit.TabIndex = 8;
this.cmdEdit.Text = "修改";
this.cmdEdit.Click += new System.EventHandler(this.cmdEdit_Click);
//
// myconnect2
//
this.myconnect2.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""F:\Select2.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(680, 273);
this.Controls.Add(this.cmdEdit);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "编辑客户数据";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// ///////////连接数据库//////////////////
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
myconnect2.Open();
MessageBox.Show("数据库连接完毕!!");
}
private void cmdEdit_Click(object sender, System.EventArgs e)
{
//string select1="true";
//string id="1";
//int id=1;
/////////////字符串中的变量应用单引号括起来/////////////
/////////////////双引号之间不应进行换行/////////////////
//string strUpdate= "UPDATE selectlab SET "+"SELECT='"+select1+"'"+"WHERE ID=" +id;
//string strUpdate= "UPDATE Select_2 SET SELECT='true' WHERE ID=1";
OleDbCommand myCmd1=myconnect2.CreateCommand();
myCmd1.CommandText="UPDATE Select_2 SET SELECT='ABCD' WHERE MYID=1";
myCmd1.ExecuteNonQuery();
MessageBox.Show("更新数据一笔!!");
}
}
}