Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/9 1:35:06
ADO.NET向数据库中添加数据

ADO.NET向数据库中添加数据

向数据库中添加数据

通过SqlCommand类的CommandText方法(为其构造insert语句)与ExecuteNonQuery方法实现向表中插入数据的功能,程序运行结果如图1与图2所示。



图1 添加信息窗体



图2 提示窗体

程序开发步骤如下。

(1)新建一个windows窗体(FrmDBoperate),在窗体上添加相应的控件(如图1所示)。

(2)在(ClsDBControl.cs)类内完成向数据库中添加数据的方法insertDB。

(3)主要程序代码如下。

首先实例化一个SqlCommand对象(cmd),然后通过参数sql(SQL 语句)来构建cmd对象。最后通过cmd对象的ExecuteNonQuery方法执行SQL语句。代码如下:

public bool insertDB(String sql)

{

using (SqlCommand cmd = new SqlCommand())

{

try

{

cmd.CommandText = sql;//设置SQL语句

cmd.Connection = ConDB();//调用打开数据库连接方法

cmd.ExecuteNonQuery();//执行

return True;

}

catch

{

return False;

}

}

}

单击【添加信息】按钮,将构造一条SQL语句(实现添加功能)。然后将这条语句传给insertDB方法,实现数据的添加。

private void button1_Click(object sender, EventArgs e)

{

ClsDB.ClsDBControl DBC = new OptDB.ClsDB.ClsDBControl();

string strSql = "insert into t_people values('" + this.textBox1.Text.Trim().ToString() + "','" +

this.textBox2.Text.Trim().ToString() + "','" + this.textBox3.Text.Trim().ToString() + "')";

if (DBC.insertDB(strSql))

{

MessageBox.Show("OK");

}

}

注意:在添加信息时,SQL中的引用变量的格式为 '" +变量+ "'。

完整程序代码如下:

★ ★★★★FrmDBoperate.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace OptDB

{

public partial class FrmDBoperate : Form

{

public FrmDBoperate()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ClsDB.ClsDBControl DBC = new OptDB.ClsDB.ClsDBControl();

string strSql = "insert into t_people values('" + this.textBox1.Text.Trim().ToString() + "','" +

this.textBox2.Text.Trim().ToString() + "','" + this.textBox3.Text.Trim().ToString() + "')";

if (DBC.insertDB(strSql))

{

MessageBox.Show("OK");

}

}

}

}

★ ★★★★FrmDBoperate.designer.cs窗体设计文件完整程序代码★★★★★

namespace OptDB

{

partial class FrmDBoperate

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.textBox3 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.textBox1 = new System.Windows.Forms.TextBox();

this.label3 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.label1 = new System.Windows.Forms.Label();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// textBox3

//

this.textBox3.Location = new System.Drawing.Point(74, 67);

this.textBox3.Name = "textBox3";

this.textBox3.Size = new System.Drawing.Size(139, 21);

this.textBox3.TabIndex = 11;

//


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759