基本信息
源码名称:access数据库参数化查询 实例 完整附源码
源码大小:0.11M
文件格式:.zip
开发语言:C#
更新时间:2013-04-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
对access数据库进行参数化查询
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace DataBase2 { /// <summary> /// 参数化查询。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; private System.Data.OleDb.OleDbConnection oleDbConnection1; private DataBase2.DataSet1 dataSet11; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox_Addr; private System.Windows.Forms.TextBox textBox_Email; private System.Windows.Forms.TextBox textBox_Name; /// <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 Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand(); this.dataSet11 = new DataBase2.DataSet1(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBox_Email = new System.Windows.Forms.TextBox(); this.textBox_Addr = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox_Name = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1; this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1; this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "user", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("username", "username"), new System.Data.Common.DataColumnMapping("address", "address"), new System.Data.Common.DataColumnMapping("Email", "Email")})}); this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1; // // oleDbDeleteCommand1 // this.oleDbDeleteCommand1.CommandText = "DELETE FROM [user] WHERE (编号 = ?) AND (Email = ? OR ? IS NULL AND Email IS NULL) " "AND (address = ? OR ? IS NULL AND address IS NULL) AND (username = ?)"; this.oleDbDeleteCommand1.Connection = this.oleDbConnection1; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_编号", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "编号", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_address", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_address1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_username", System.Data.OleDb.OleDbType.VarWChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "username", System.Data.DataRowVersion.Original, null)); // // oleDbConnection1 // //this.oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=E:\Project\DataBase2\bin\Debug\test.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; this.oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" System.Windows.Forms.Application.StartupPath @"\test.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; // // oleDbInsertCommand1 // this.oleDbInsertCommand1.CommandText = "INSERT INTO [user] (username, address, Email, 编号) VALUES (?, ?, ?, ?)"; this.oleDbInsertCommand1.Connection = this.oleDbConnection1; this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("username", System.Data.OleDb.OleDbType.VarWChar, 20, "username")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("address", System.Data.OleDb.OleDbType.VarWChar, 50, "address")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Email", System.Data.OleDb.OleDbType.VarWChar, 50, "Email")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("编号", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "编号", System.Data.DataRowVersion.Current, null)); // // oleDbSelectCommand1 // this.oleDbSelectCommand1.CommandText = "SELECT username, address, Email, 编号 FROM [user] WHERE (username = ?)"; this.oleDbSelectCommand1.Connection = this.oleDbConnection1; this.oleDbSelectCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("username", System.Data.OleDb.OleDbType.VarWChar, 20, "username")); // // oleDbUpdateCommand1 // this.oleDbUpdateCommand1.CommandText = "UPDATE [user] SET username = ?, address = ?, Email = ?, 编号 = ? WHERE (编号 = ?) AND" " (Email = ? OR ? IS NULL AND Email IS NULL) AND (address = ? OR ? IS NULL AND ad" "dress IS NULL) AND (username = ?)"; this.oleDbUpdateCommand1.Connection = this.oleDbConnection1; this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("username", System.Data.OleDb.OleDbType.VarWChar, 20, "username")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("address", System.Data.OleDb.OleDbType.VarWChar, 50, "address")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Email", System.Data.OleDb.OleDbType.VarWChar, 50, "Email")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("编号", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "编号", System.Data.DataRowVersion.Current, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_编号", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "编号", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_address", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_address1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_username", System.Data.OleDb.OleDbType.VarWChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "username", System.Data.DataRowVersion.Original, null)); // // dataSet11 // this.dataSet11.DataSetName = "DataSet1"; this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN"); this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(67, 18); this.label1.TabIndex = 0; this.label1.Text = "用户名:"; // // button1 // this.button1.Location = new System.Drawing.Point(264, 16); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "查询"; this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.button1.Click = new System.EventHandler(this.button1_Click); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox_Email, this.textBox_Addr, this.label3, this.label2}); this.groupBox1.Location = new System.Drawing.Point(8, 56); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(336, 200); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = " 查询结果 "; // // textBox_Email // this.textBox_Email.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "user.Email")); this.textBox_Email.Location = new System.Drawing.Point(56, 152); this.textBox_Email.Name = "textBox_Email"; this.textBox_Email.Size = new System.Drawing.Size(256, 25); this.textBox_Email.TabIndex = 3; this.textBox_Email.Text = ""; // // textBox_Addr // this.textBox_Addr.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "user.address")); this.textBox_Addr.Location = new System.Drawing.Point(56, 72); this.textBox_Addr.Name = "textBox_Addr"; this.textBox_Addr.Size = new System.Drawing.Size(256, 25); this.textBox_Addr.TabIndex = 2; this.textBox_Addr.Text = ""; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(16, 120); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(60, 18); this.label3.TabIndex = 1; this.label3.Text = "Email:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(16, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(52, 18); this.label2.TabIndex = 0; this.label2.Text = "地址:"; // // textBox_Name // this.textBox_Name.Location = new System.Drawing.Point(88, 16); this.textBox_Name.Name = "textBox_Name"; this.textBox_Name.Size = new System.Drawing.Size(144, 25); this.textBox_Name.TabIndex = 4; this.textBox_Name.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(8, 18); this.ClientSize = new System.Drawing.Size(354, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox_Name, this.groupBox1, this.button1, this.label1}); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "参数化查询"; ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { if (textBox_Name.Text.Length != 0) { oleDbDataAdapter1.SelectCommand.Parameters["username"].Value = textBox_Name.Text; dataSet11.Clear(); oleDbDataAdapter1.Fill(dataSet11); } } } }