WPF簡(jiǎn)單的數(shù)據(jù)庫查詢實(shí)例
做一個(gè)簡(jiǎn)單WPF連接數(shù)據(jù)庫的
控件類型和名稱:
DataGrid:dataGrid
Button1 :Button1
Button :Button2
TextBox :txtuserName
在引用App.config寫數(shù)據(jù)庫的連接字符串
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <!--數(shù)據(jù)庫連接字符串--> <add key ="ConnString" value ="Data Source=.;initial Catalog=educ; user=sa; Password=123456;Pooling=true" /> </appSettings> </configuration>
<add key ="ConnString" value ="Data Source=.;initial Catalog=educ; user=sa; Password=123456;Pooling=true" />
Data Source=.表示本機(jī),可以寫ip地址 initial Catalog=數(shù)據(jù)庫名 user=用戶名 Password=密碼;寫一個(gè)DataBaseHelper的數(shù)據(jù)庫類
namespace _03連接數(shù)據(jù)庫 { class DataBaseHelper { /// 數(shù)據(jù)庫打開連接的方法 /// /// </summary> /// <returns></returns> public static SqlConnection getSqlConnection() { SqlConnection sqlConnection = new SqlConnection(); try { //獲取數(shù)據(jù)庫字符串 sqlConnection.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnString"]; sqlConnection.Open(); sqlConnection.Close(); } catch { throw new Exception("無法連接數(shù)據(jù)庫服務(wù)器"); } return sqlConnection; } /// sql增刪改的方法 /// /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static int GetNonQueryEffect(string sqlstr) { SqlConnection sqlConnection = new SqlConnection(); try { sqlConnection.Open(); //創(chuàng)建要執(zhí)行的語句 SqlCommand cmd = new SqlCommand(sqlstr, sqlConnection); return cmd.ExecuteNonQuery();//返回執(zhí)行語句中的錯(cuò)誤 } catch (Exception ex) { throw new Exception(ex.ToString()); } finally { sqlConnection.Close(); sqlConnection.Dispose();//釋放資源 } } /// 讀取數(shù)據(jù)的的方法 /// /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public static DataSet GetDataset(string sqlstr) { SqlConnection conn = getSqlConnection(); try { conn.Open();//打開數(shù)據(jù)庫連接 SqlDataAdapter sda = new SqlDataAdapter(sqlstr ,conn );//更新數(shù)據(jù)庫的命令 DataSet ds = new DataSet(); sda.Fill(ds);//填充 return ds; } catch (Exception ex) { throw new Exception(ex.ToString ()); } finally { conn.Close(); conn.Dispose(); } } } }
按鍵的代碼
private void Button_Click_1(object sender, RoutedEventArgs e) { string str = "select *FROM student";//查詢的語句 dataGrid.ItemsSource = DataBaseHelper.GetDataset(str).Tables[0].DefaultView; } private void Button_Click_2(object sender, RoutedEventArgs e) { if (txtuserName.Text.Trim()== " ") { return; } string strr = string.Format("select *FROM student where sname='{0}'", txtuserName.Text); dataGrid.ItemsSource = DataBaseHelper.GetDataset(strr).Tables[0].DefaultView; }
以上這篇WPF簡(jiǎn)單的數(shù)據(jù)庫查詢實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#中如何自定義配置上周和本周起始日來查詢業(yè)務(wù)數(shù)據(jù)(思路詳解)
在C#中并沒有封裝的方法根據(jù)我們需要來直接獲取上一周某天到某天、本周某天到某天,所以需要我們自己封裝方法來實(shí)現(xiàn)(我們也可以按照這個(gè)思路使用其他語言來實(shí)現(xiàn)),感興趣的朋友跟隨小編一起看看吧2023-09-09C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法,以簡(jiǎn)單實(shí)例形式簡(jiǎn)單分析了C#實(shí)現(xiàn)數(shù)據(jù)綁定與讀取的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08快速了解如何在.NETCORE中使用Generic-Host建立主機(jī)
這篇文章主要介紹了如何在.NETCORE中使用Generic-Host建立主機(jī),文中代碼非常詳細(xì),可供大家參考,感興趣的朋友不妨閱讀完2020-05-05C#中接口的顯式實(shí)現(xiàn)與隱式實(shí)現(xiàn)及其相關(guān)應(yīng)用案例詳解
最近在學(xué)習(xí)演化一款游戲項(xiàng)目框架時(shí)候,框架作者巧妙使用接口中方法的顯式實(shí)現(xiàn)來變相對(duì)接口中方法進(jìn)行“密封”,增加實(shí)現(xiàn)接口的類訪問方法的“成本”,這篇文章主要介紹了C#中接口的顯式實(shí)現(xiàn)與隱式實(shí)現(xiàn)及其相關(guān)應(yīng)用案例,需要的朋友可以參考下2024-05-05WPF實(shí)現(xiàn)授權(quán)碼顯示密文并支持換行
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(xiàn)授權(quán)碼顯示密文并支持換行,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-10-10