WPF简单的数据库查询实例
作者:happy多乐 时间:2024-01-15 07:25:31
做一个简单WPF连接数据库的
控件类型和名称:
DataGrid:dataGrid
Button1 :Button1
Button :Button2
TextBox :txtuserName
在引用App.config写数据库的连接字符串
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<!--数据库连接字符串-->
<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=.表示本机,可以写ip地址 initial Catalog=数据库名 user=用户名 Password=密码;写一个DataBaseHelper的数据库类
namespace _03连接数据库
{
class DataBaseHelper
{
/// 数据库打开连接的方法
///
/// </summary>
/// <returns></returns>
public static SqlConnection getSqlConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
//获取数据库字符串
sqlConnection.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
sqlConnection.Open();
sqlConnection.Close();
}
catch
{
throw new Exception("无法连接数据库服务器");
}
return sqlConnection;
}
/// sql增删改的方法
///
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static int GetNonQueryEffect(string sqlstr)
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.Open();
//创建要执行的语句
SqlCommand cmd = new SqlCommand(sqlstr, sqlConnection);
return cmd.ExecuteNonQuery();//返回执行语句中的错误
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
sqlConnection.Close();
sqlConnection.Dispose();//释放资源
}
}
/// 读取数据的的方法
///
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataSet GetDataset(string sqlstr)
{
SqlConnection conn = getSqlConnection();
try
{
conn.Open();//打开数据库连接
SqlDataAdapter sda = new SqlDataAdapter(sqlstr ,conn );//更新数据库的命令
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;
}
来源:http://www.cnblogs.com/enduo/p/7793907.html
标签:WPF,数据库,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP处理XSLT转换XML的实现
2008-10-20 18:37:00
python实现飞船大战
2022-09-19 02:17:26
![](https://img.aspxhome.com/file/2023/3/82583_0s.gif)
10个Python小技巧你值得拥有
2022-02-03 16:28:56
详解Python不同版本之间的切换方法
2023-12-28 05:22:06
![](https://img.aspxhome.com/file/2023/9/124689_0s.png)
python二叉树遍历的实现方法
2021-09-19 03:53:14
vue-element-admin 登陆及目录权限控制的实现
2024-04-28 09:26:29
![](https://img.aspxhome.com/file/2023/2/133022_0s.jpg)
浅谈Tensorflow 动态双向RNN的输出问题
2022-10-16 21:30:35
![](https://img.aspxhome.com/file/2023/3/97733_0s.jpg)
python使用json.dumps输出中文问题
2023-11-17 22:04:46
![](https://img.aspxhome.com/file/2023/8/71428_0s.jpg)
简单了解Javscript中兄弟ifream的方法调用
2024-04-22 22:29:30
数据库理论:学习基于SQL数据库的算法
2009-01-13 13:38:00
Python如何匹配文本并在其上一行追加文本
2022-10-19 13:16:43
![](https://img.aspxhome.com/file/2023/2/99122_0s.png)
Nodejs中session的简单使用及通过session实现身份验证的方法
2024-05-13 10:06:16
如何使用Python对Excel表格进行拼接合并
2021-03-15 18:01:54
![](https://img.aspxhome.com/file/2023/9/76039_0s.png)
vue 虚拟DOM的原理
2023-07-02 17:03:18
![](https://img.aspxhome.com/file/2023/5/139905_0s.png)
原生javascript AJAX 三级联动的实现代码
2024-04-18 10:00:46
python 详解如何写flask文件下载接口
2023-05-04 21:07:06
![](https://img.aspxhome.com/file/2023/9/68929_0s.png)
Python实现带参数的用户验证功能装饰器示例
2023-05-15 01:34:05
php 的反射详解及示例代码
2024-06-05 09:46:28
深入理解Python中range和xrange的区别
2023-06-22 06:10:09
Python 文件管理实例详解
2022-08-22 16:29:54