VS2019 C# 连接IBM DB2数据库并查询数据表

操作系统:win10 64位

C#:VS2019

数据库:IBM DB2  v11.1_win64_expc

其他工具:IBM Data Studio  ibm_ds4130_win

步骤一:使用命令行或是IBM DB2工具:Data Studio,创建一个数据库。这里名称为:Student

CREATE DATABASE "Student" AUTOMATIC STORAGE YES ON 'D:' DBPATH ON 'D:'

 

步骤二:创建使用的数据表,这里建一个简单的学生信息管理表,TB_STUDENT

DROP TABLE "DB2ADMIN"."TB_STUDENT";

CREATE TABLE "DB2ADMIN"."TB_STUDENT" (
		"学生编号" INTEGER NOT NULL, 
		"学生姓名" VARCHAR(20 OCTETS), 
		"性别" VARCHAR(20 OCTETS), 
		"出生年月" VARCHAR(50 OCTETS), 
		"所在学院" VARCHAR(50 OCTETS), 
		"所学专业" VARCHAR(50 OCTETS), 
		"年龄" VARCHAR(20 OCTETS), 
		"籍贯" VARCHAR(20 OCTETS), 
		"家庭住址" VARCHAR(100 OCTETS), 
		"备注信息" VARCHAR(100 OCTETS), 
		"统招否" VARCHAR(10 OCTETS), 
		"身份证" VARCHAR(50 OCTETS)
	)
	ORGANIZE BY ROW
	DATA CAPTURE NONE 
	IN "USERSPACE1"
	COMPRESS NO;

 

步骤三:打开VS2019(其他的如VS2013等,也是可以的),这里选择C#编程语言。

主要代码:top.cs,用于查看数据表内容

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;


namespace SQL_003
{
    public partial class Top : Form
    {
        public Top()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetMessage(dgv_Message);
        }

        /// 
        /// 查询数据库相关信息
        /// 
        ///方法返回DataTable对象
        private void GetMessage(DataGridView dg)
        {
            string strConnForDB2 = "Provider=IBMDADB2;DataSource=192.168.1.3;Database=Student;User ID=db2admin;Password=admin888;protocol=TCPIP;port=50000;";
            /*
            OleDbConnection conStr = new OleDbConnection(
            @"Provider=IBMDADB2;Data Source=localhost;UID=db2admin;PWD=admin888;" );
             */
            OleDbConnection conStr = new OleDbConnection(strConnForDB2);
            string P_Str_SqlStr = string.Format(//创建Sql查询字符串
             "select * from \"TB_STUDENT\"");
            conStr.Open();
            OleDbCommand cmd = new OleDbCommand(P_Str_SqlStr, conStr);
            OleDbDataAdapter P_OleDataAdapter = new OleDbDataAdapter(cmd);
            DataTable P_dt = new DataTable();//创建数据表
            P_OleDataAdapter.Fill(P_dt);    //填充数据表
            dg.DataSource = P_dt;        //返回数据表
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetMessage(dgv_Message);
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            Add form = new Add();
            form.Show();
        }
    }
}

 

add.cs,用于添加数据

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace SQL_003
{
    public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str_Name, str_Sex, str_School, str_Subject, str_IdNum;
            string str_Age, str_Native, str_FAddr, str_Remark, str_Tongzhao;
            string str_Birthday;
            int nID;
            if (txt_Id.Text == "")
            {
                MessageBox.Show("学生编号不能为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txt_Id.Focus();
                return;
            }
            if (txt_Name.Text == "")
            {
                MessageBox.Show("学生姓名不能为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txt_Name.Focus();
                return;
            }
            nID = Convert.ToInt32(txt_Id.Text);
            str_IdNum=txt_IDnum.Text;
            str_Name=txt_Name.Text;
            str_Subject=txt_Subject.Text;
            str_School=txt_School.Text;

            str_Age = txt_Age.Text;
            str_Remark = txt_Remark.Text;
            str_Native = txt_Native.Text;
            str_FAddr = txt_FAddr.Text;

            str_Birthday = dtp_Birthday.Value.ToString("yyyy-MM-dd"); 
            if (cb_Tongzhao.Checked)
            {
                str_Tongzhao = "是";
            }
            else
            {
                str_Tongzhao = "否";
            }

            if (rbt_Male.Checked)
            {
                str_Sex = "男";
            }
            else
            {
                str_Sex = "女";
            }

            string strConnForDB2 = "Provider=IBMDADB2;DataSource=192.168.1.3;Database=Student;User ID=db2admin;Password=admin888;protocol=TCPIP;port=50000;";

            string P_Str_SqlStr = "INSERT INTO \"TB_STUDENT\"(学生编号,学生姓名,性别,出生年月,";
            P_Str_SqlStr += "所在学院,所学专业,年龄,籍贯,家庭住址,备注信息,统招否,身份证) values (";
            P_Str_SqlStr += nID + ", '";
            P_Str_SqlStr += str_Name + "', '";
            P_Str_SqlStr += str_Sex + "','";
            P_Str_SqlStr += str_Birthday +"','";
            P_Str_SqlStr += str_School + "', '";
            P_Str_SqlStr += str_Subject + "', '";

            P_Str_SqlStr += str_Age + "', '";
            P_Str_SqlStr += str_Native + "', '";
            P_Str_SqlStr += str_FAddr + "', '";
            P_Str_SqlStr += str_Remark + "', '";
            P_Str_SqlStr += str_Tongzhao + "', '";
            P_Str_SqlStr += str_IdNum + "')";

            OleDbConnection conStr = new OleDbConnection(strConnForDB2);
            OleDbCommand cmd = new OleDbCommand(P_Str_SqlStr, conStr);
            try
            {
                conStr.Open();
                cmd.ExecuteNonQuery();
                conStr.Close();
                MessageBox.Show("数据添加成功!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据添加失败!"+ex, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();       //关闭当前的窗口,主窗口不关闭。
        }
    }
}

 

窗体设计:C#是一个优秀的界面设计工具,界面比VC++简单很多,强很多。

 

VS2019 C# 连接IBM DB2数据库并查询数据表_第1张图片

 

VS2019 C# 连接IBM DB2数据库并查询数据表_第2张图片

 

这样运行起来,添加数据,应该可以显示数据了。接下来,可以继续增加修改与删除功能。

VS2019 C# 连接IBM DB2数据库并查询数据表_第3张图片

 

总结:DB2连接数据库,其实有好几种方法,还有一种是使用DB2的DLL文件提供的连接方法。下一次测试整理好再写。

 

工程下载地址:https://gitee.com/fsmd/DB2_001.git

你可能感兴趣的:(C#)