wpf数据库连接登陆案例
来源:互联网 发布:java短链接 编辑:程序博客网 时间:2024/06/06 00:55
一、窗体布置
<Window x:Class="Sql.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="增加" Name="insert" HorizontalAlignment="Left" Margin="111,85,0,0" VerticalAlignment="Top" Width="75" Click="insert_Click"/>
<Button Content="删除" Name="delete" HorizontalAlignment="Left" Margin="111,135,0,0" VerticalAlignment="Top" Width="75" Click="delete_Click"/>
<Button Content="修改" Name="update" HorizontalAlignment="Left" Margin="277,85,0,0" VerticalAlignment="Top" Width="75" Click="update_Click"/>
<Button Content="查询" Name="select" HorizontalAlignment="Left" Margin="277,135,0,0" VerticalAlignment="Top" Width="75" Click="select_Click"/>
</Grid>
</Window>
二、增删改查
using System;
using System.Collections.Generic;
using System.Data.SqlClient; //添加sql客户端引用
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Sql
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void insert_Click(object sender, RoutedEventArgs e)
{
//连接数据库
String conStr = "Data Source = localhost;Initial Catalog = studentDatabase;User Id = sa;Password = 123";
//创建数据库连接对象
SqlConnection con = new SqlConnection(conStr);
//打开连接
con.Open();
//创建SQL命令对象
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into student (studentNo,studentName) values ('002','张三')";
//执行命令
int count = cmd.ExecuteNonQuery(); //增删改用这种方式
MessageBox.Show("受影响的行数:" + count);
//关闭连接
con.Close();
}
private void update_Click(object sender, RoutedEventArgs e)
{
String conStr = "Data Source = localhost;Initial Catalog = studentDatabase;User Id = sa;Password = 123";
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "update student set studentName='小华' where studentNo=001";
com.ExecuteNonQuery();
con.Close();
}
private void delete_Click(object sender, RoutedEventArgs e)
{
String conStr = "Data Source = localhost;Initial Catalog = studentDatabase;User Id = sa;Password = 123";
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "delete from student where studentNo='002'";
com.ExecuteNonQuery();
con.Close();
}
private void select_Click(object sender, RoutedEventArgs e)
{
String conStr = "Data Source = localhost;Initial Catalog = studentDatabase;User Id = sa;Password = 123";
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "select *from student";
SqlDataReader read=com.ExecuteReader();
while (read.Read())
{
string studentNo = read.GetString(0);
string studentName = read.GetString(1);
int studentScore = read.GetInt32(2);
MessageBox.Show(string.Format("姓名:{0},学号:{1},成绩:{2}", studentName, studentNo,studentScore));
con.Close();
}
}
}
}
代码:http://download.csdn.net/detail/gj_user/9714254
- wpf数据库连接登陆案例
- 登陆案例
- 登陆案例
- WPF与MySql数据库连接
- Wpf 实现登陆窗口
- Servlet与数据库连接案例
- WPF Command 案例
- WPF 命令参数案例
- servlet 登陆注销案例
- Struts2登陆案例
- WebService的登陆案例
- springmvc登陆拦截案例
- xpath登陆案例
- java-登陆注册案例
- Struts2登陆案例
- WPF多窗口综合案例
- Struts2入门之登陆案例
- 用户登陆注册案例分析
- HBase总结(十四)LINUX下用Eclipse构建HBase开发环境
- 机器学习方法:使用Apriori算法和FP-growth算法进行关联分析(Python版)
- 数组中两个字符串的最小距离
- Destroy the Well of Life HDU - 1692
- Android Studio插件整理
- wpf数据库连接登陆案例
- poj1258 Agri-Net 最小生成树
- Eclipse中编写Java调用C代码
- 观察者模式
- C# using 三种使用方式
- 二维码操作工具类
- nginx+php+gitblog
- Android LayoutInflater详解
- Header V3 DSA signature: NOKEY, key ID***** 错误解决、依赖包解决方法