金仓数据库KingbaseES V8 R6-VS2017.NET 数据库连接范例





App.config.cs
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="strcon" connectionString="Server=192.168.45.130;User Id=system;Password = 123456; Database = test; Port = 54321"/>
</connectionStrings>
</configuration>
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();
}
}
}
}

创建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
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)