C# 如何用Access数据库字段填充combobox?

怎样绑定数据库,能否给出个示例代码... 怎样绑定数据库,能否给出个示例代码 展开
 我来答
hehai522127
2012-04-24 · 超过62用户采纳过TA的回答
知道小有建树答主
回答量:197
采纳率:0%
帮助的人:102万
展开全部
这个跟SQL SERVER 一样。只是命名空间不一样
下面的代码供你参考。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace SQLHelper
{
public class AccessDBHlper
{
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetOleDbConnection()
{
//String conn = ""; 链接字符串
String conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
return conn;
}

/// <summary>
/// 获取动态字符串
/// </summary>
/// <param name="constr">连接字符串名称</param>
/// <returns></returns>
public static OleDbConnection sc(string constr)
{
OleDbConnection sc = new OleDbConnection(constr);
sc.Open();
return sc;
}

/// <summary>
/// 获取连接对象
/// </summary>
/// <returns></returns>
public static OleDbConnection GetCon()
{
OleDbConnection sc = new OleDbConnection(GetOleDbConnection());
sc.Open();
return sc;
}

/// <summary>
/// 获得参数对象
/// </summary>
/// <param name="paramName">茄铅参数名称</param>
/// <param name="paramType">数据类型</param>此纳禅
/// <param name="paramSize">长森尘度</param>
/// <param name="ColName">源列名称</param>
/// <returns></returns>
public static OleDbParameter GetParameter(String paramName, SqlDbType paramType, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName, paramType);
param.Value = paramValue;
return param;
}

public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
int counter;

counter = cmd.ExecuteNonQuery();

conn.Close();
return counter;
}
}

/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, OleDbParameter[] param)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
int counter;
counter = cmd.ExecuteNonQuery();

conn.Close();
return counter;
}
}
/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetOleDbConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbDataAdapter da = new OleDbDataAdapter(Sqlstr, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式