C# SqlHelper應(yīng)用開(kāi)發(fā)學(xué)習(xí)
本文實(shí)例為大家分享了C# SqlHelper應(yīng)用技巧,供大家參考,具體內(nèi)容如下
使用App.config配置文件封裝連接字符串,方便重復(fù)使用
--->添加App.conifg配置文件
--->Add : ConnectionString:
--->添加引用
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="conStr" connectionString="Data Source=.;Initial Catalog=;User ID=;Password="/> </connectionStrings> </configuration>
封裝一個(gè)SQLHelper類方便使用
using System.Configuration;
using System.Data;//DatSet..Table SqlDataAdapter
using System.Data.SqlClient;//SqlConnection Command DataReader
namespace Common
{
public class SqlHelper
{
//連接字符串
//1、添加引用 2、導(dǎo)入命名空間 為了使用ConfigurationManager
private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
//增刪改查
//查找數(shù)據(jù) ExecuteScalar()返回首行首列 ExecuteReader() DataTable
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql">所用的sql語(yǔ)句</param>
/// <param name="param">可變,可以傳參也可以不傳參數(shù)</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] param)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, con))
{
//添加參數(shù)
adapter.SelectCommand.Parameters.AddRange(param);
//1.打開(kāi)鏈接,如果連接沒(méi)有打開(kāi),則它給你打開(kāi);如果打開(kāi),就算了
//2.去執(zhí)行sql語(yǔ)句,讀取數(shù)據(jù)庫(kù)
//3.sqlDataReader,把讀取到的數(shù)據(jù)填充到內(nèi)存表中
adapter.Fill(dt);
}
}
return dt;
}
/// <summary>
/// 執(zhí)行查詢,返回首行首列
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, params SqlParameter[] param)
{
object o = null;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
o = cmd.ExecuteScalar();
}
}
return o;
}
/// <summary>
/// 執(zhí)行查詢,返回SqlDataReader對(duì)象
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param)
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
reader = cmd.ExecuteReader();
}
}
return reader;
}
/// <summary>
/// 執(zhí)行增刪改,返回受影響的行數(shù)
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] param)
{
int n = -1;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
n = cmd.ExecuteNonQuery();
}
}
return n;
}
}
}
C#using三種使用方法: http://www.dbjr.com.cn/article/102855.htm
C#namespace: 是為了防止命名重復(fù)的 。
比如你在兩個(gè)不同的命名空間中都可以有Student類。
此命名空間范圍允許您組織代碼并為您提供了創(chuàng)建全局唯一類型的方法。
C#中Sqlparamater的用法:http://www.dbjr.com.cn/article/101015.htm
下面這個(gè)是應(yīng)用sqlHelper和ComboBox來(lái)展示 省市聯(lián)動(dòng):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//在應(yīng)用程序加載的時(shí)候 ,去數(shù)據(jù)庫(kù)查找省的數(shù)據(jù),給cboPro
DataTable dt = SqlHelper.ExecuteDataTable("select * from promary");
//將返回的DataTable作為cboPro的數(shù)據(jù)源
//讓cboPro顯示proName這個(gè)字段的值,一般是顯示給客戶看的
cboPro.DisplayMember = "proName";
//讓valueMemberID,綁定的是對(duì)應(yīng)的值,綁定處理程序標(biāo)識(shí) 給程序員看的。
cboPro.ValueMember = "proID";
cboPro.DataSource = dt;
}
private void cboPro_SelectedIndexChanged(object sender, EventArgs e)
{
//之前的寫法
// MessageBox.Show(cboPro.Text);//獲得在cbo中選擇文本
//MessageBox.Show(cboPro.SelectedValue.ToString());//獲得關(guān)聯(lián)的數(shù)據(jù)
//string sql = "select * from city where proID="+cboPro.SelectedValue.ToString();
//帶參數(shù)的sql語(yǔ)句
string sql = "select * from city where proID=@proID";
//準(zhǔn)備一個(gè)sql參數(shù)
SqlParameter p = new SqlParameter("@proID", cboPro.SelectedValue.ToString());
//設(shè)置cboCity要顯示的數(shù)據(jù)
cboCity.DisplayMember = "cityName";
//根據(jù)sql語(yǔ)句查詢到的數(shù)據(jù)集
cboCity.DataSource = SqlHelper.ExecuteDataTable(sql, p);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)操作MySql數(shù)據(jù)層類MysqlHelper實(shí)例
- C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫(kù)的方法
- 詳解使用C#編寫SqlHelper類
- C#編寫SqlHelper類
- C#實(shí)現(xiàn)較為實(shí)用的SQLhelper
- c#中SqlHelper封裝SqlDataReader的方法
- C# Oracle數(shù)據(jù)庫(kù)操作類實(shí)例詳解
- c#連接access數(shù)據(jù)庫(kù)操作類分享
- C#數(shù)據(jù)庫(kù)操作類AccessHelper實(shí)例
- C#實(shí)現(xiàn)的ACCESS數(shù)據(jù)庫(kù)操作類完整實(shí)例
- C#實(shí)現(xiàn)的封裝CURD到SqlHelper類用法簡(jiǎn)單分析
相關(guān)文章
Winform?控件優(yōu)化LayeredWindow無(wú)鋸齒圓角窗體
這篇文章主要為大家介紹了Winform?控件優(yōu)化LayeredWindow實(shí)現(xiàn)無(wú)鋸齒圓角窗體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
C# 中 System.Index 結(jié)構(gòu)體和 Hat 運(yùn)算符(^)的使用示例
這篇文章主要介紹了C# 中 System.Index 結(jié)構(gòu)體和 Hat 運(yùn)算符(^)的使用示例,幫助大家更好的理解和使用C#,感興趣的朋友可以了解下2020-09-09
C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法
QQ軟件對(duì)于絕大多數(shù)的人來(lái)說(shuō)再熟悉不過(guò)了,它以使用方便、界面美觀及功能完善而著稱,本文給大家介紹了C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法,主要通過(guò)使用API函數(shù)WindowFromPoint和GetParent實(shí)現(xiàn)仿QQ的抽屜式窗體,需要的朋友可以參考下2024-04-04
C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫(kù)的方法,通過(guò)自定義函數(shù)db_connection_test實(shí)現(xiàn)遠(yuǎn)程連接Oracle數(shù)據(jù)庫(kù)的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12

