-
VS2017/金仓数据库管理系统V8.0 R6 使用
-
测试代码链接: https://pan.baidu.com/s/1M6R-MF74ghyCnXuQqhcMZQ 提取码: mfch
- 金仓官网下载驱动
-
- 以net40为例
- VS2017新建项目
- 新建项目
-
- 引用驱动
-
- 参数配置
App.config.cs
- VS2017测试项目
- 创建一个数据库类
KEsDB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using Kdbndp;
namespace WindowsFormsApp2
{
class KesDB
{
private string _sqlConn = "";
private Kdbndp.KdbndpConnection _conn = null;
private KdbndpCommand _cmd = null;
private KdbndpDataAdapter _adp = null;
public KesDB()
{
_sqlConn = System.Configuration.ConfigurationManager.ConnectionStrings["strcon"].ToString();
}
public void openDb()//打开数据库
{
try
{
_conn = new KdbndpConnection(_sqlConn);
_conn.Open();
}
catch (Exception ex)
{
throw ex;
}
}
public void closeDb()//关闭数据库
{
try
{
_conn.Close();
_conn = null;
}
catch(Exception ex)
{
throw ex;
}
}
public void sqlCommandExcute(string sqlstr)//SQL执行
{
openDb();
try {
Kdbndp.KdbndpCommand kcmd = new KdbndpCommand(sqlstr, _conn);
kcmd.ExecuteNonQuery();
}
finally {
closeDb();
}
}
public void sqlDataAdapter(string sqlstr,DataSet ds)//sqlDataAdapter
{
openDb();
try
{
KdbndpDataAdapter kadp = new KdbndpDataAdapter(sqlstr, _conn);
kadp.Fill(ds);
}
finally
{
closeDb();
}
}
}
}
-
- 创建一个窗体
-
- 数据创建user表
创建2个字段:
CREATE TABLE "public"."user" (
"id" varchar(30) NULL,
"name" varchar(30) NULL
);
-
- 窗体代码
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.Configuration;
namespace WindowsFormsApp2
{
public partial class frm_user : Form
{
public frm_user()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void texbnull()
{
texB_userID.Text = "";
texB_name.Text = "";
texB_userID.Focus();
}
private void QueryAllUser()
{
KesDB kes = new KesDB();
kes.openDb();
DataSet ds = new DataSet();
kes.sqlDataAdapter("select * from public.user where id='" + texB_name.Text+"'",ds);
dGV_user.DataSource = ds.Tables[0];
dGV_user.Columns[0].HeaderText = "用户编号";
dGV_user.Columns[1].HeaderText = "用户名称";
}
private void frm_user_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string userid = texB_userID.Text;
string username = texB_name.Text;
if (userid == "" || username == "")
{
MessageBox.Show("关键信息不能为空!");
}
else
{
KesDB kes = new KesDB();
kes.openDb();
kes.sqlCommandExcute("insert into public.user values('"+userid+"','"+username+"')");
QueryAllUser();
toolStripStatusLabel1.Text = username + "--添加成功--";
texbnull();
}
}
private void frm_user_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void sta_text_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void dGv_user_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
private void frm_user_Activated(object sender, EventArgs e)
{
texB_userID.Focus();
}
}
}
更多人大金仓数据库信息,详见:https://help.kingbase.com.cn



