C# SqlHelper應(yīng)用開發(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語句</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.打開鏈接,如果連接沒有打開,則它給你打開;如果打開,就算了
//2.去執(zhí)行sql語句,讀取數(shù)據(jù)庫
//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://m.fzitv.net/article/102855.htm
C#namespace: 是為了防止命名重復(fù)的 。
比如你在兩個(gè)不同的命名空間中都可以有Student類。
此命名空間范圍允許您組織代碼并為您提供了創(chuàng)建全局唯一類型的方法。
C#中Sqlparamater的用法:http://m.fzitv.net/article/101015.htm
下面這個(gè)是應(yīng)用sqlHelper和ComboBox來展示 省市聯(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ù)庫查找省的數(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語句
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語句查詢到的數(shù)據(jù)集
cboCity.DataSource = SqlHelper.ExecuteDataTable(sql, p);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)操作MySql數(shù)據(jù)層類MysqlHelper實(shí)例
- C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫的方法
- 詳解使用C#編寫SqlHelper類
- C#編寫SqlHelper類
- C#實(shí)現(xiàn)較為實(shí)用的SQLhelper
- c#中SqlHelper封裝SqlDataReader的方法
- C# Oracle數(shù)據(jù)庫操作類實(shí)例詳解
- c#連接access數(shù)據(jù)庫操作類分享
- C#數(shù)據(jù)庫操作類AccessHelper實(shí)例
- C#實(shí)現(xiàn)的ACCESS數(shù)據(jù)庫操作類完整實(shí)例
- C#實(shí)現(xiàn)的封裝CURD到SqlHelper類用法簡單分析
相關(guān)文章
操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼
這篇文章主要介紹了操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼,有需要的朋友可以參考一下2013-11-11
Unity 使用TexturePacker打包圖集的操作方法
這篇文章主要介紹了Unity 使用TexturePacker打包圖集的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
C#?HttpClient超時(shí)重試機(jī)制詳解
超時(shí)重試的實(shí)現(xiàn)方式可以使用循環(huán)結(jié)構(gòu),在請(qǐng)求發(fā)起后等待一定時(shí)間,若超時(shí)未收到響應(yīng),則再次發(fā)起請(qǐng)求,循環(huán)次數(shù)可以根據(jù)實(shí)際情況進(jìn)行設(shè)置,一般建議不超過三次,這篇文章主要介紹了C#?HttpClient超時(shí)重試,需要的朋友可以參考下2023-06-06

