1、MainWindow.xaml 面板
<Window x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Test" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" WindowState="Maximized"> <Grid > <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="文件"> <MenuItem Header="打开"></MenuItem> </MenuItem> <MenuItem Header="编辑"> <MenuItem Header="复制"></MenuItem> </MenuItem> <MenuItem x:Name="About" Header="关于我们" Click="About_Click"></MenuItem> <MenuItem x:Name="Update" Header="更新" Click="Update_Click"></MenuItem> <MenuItem x:Name="Chaxun" Header="查询1" Click="Chaxun_Click"></MenuItem> <MenuItem x:Name="Chaxun2" Header="查询2" Click="Chaxun2_Click"></MenuItem> </Menu> <TextBox DockPanel.Dock="Left"></TextBox> <TextBox DockPanel.Dock="Right"></TextBox> <TextBox DockPanel.Dock="Bottom"></TextBox> <Button Content="左" DockPanel.Dock="Left"></Button> <Button Content="右" DockPanel.Dock="Right"></Button> </DockPanel> </Grid> </Window>
2、MainWindow.xaml.cs 事件
using System; using System.Collections.Generic; using System.Data.SqlClient; 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; using System.Collections; using System.Data; namespace Test { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void About_Click(object sender, RoutedEventArgs e) { //AboutWindow abw = new AboutWindow(); //abw.ShowDialog(); SqlHelper sh = new SqlHelper(); SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@name", "赵六"), new SqlParameter("@id", "1") }; string sql = "UPDATE user1 SET [email protected] WHERE [email protected]"; int count = sh.ExcuteSQLReturnInt(sql, pars); MessageBox.Show("更新了 "+count+" 条数据"); } private void Update_Click(object sender, RoutedEventArgs e) { //连接字符串(可以在左边服务器资源管理器里自动生成): //Data source=服务器名(IP地址)\实例名; //Initial Catalog=数据库名; //Integrated Security=True 集成身份验证 //User ID=xxx;Password=xxx 用户名密码登陆 string constr = @"Data source=LENOVO-PC;Initial Catalog=test;Integrated Security=False;User ID=sa;Password=root"; using (SqlConnection con = new SqlConnection(constr)) { string sql = "UPDATE user1 SET name=‘wwwww‘ WHERE id=1 "; using (SqlCommand cmd = new SqlCommand(sql, con)) { if (con.State == System.Data.ConnectionState.Closed) { con.Open(); } int r = cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("已成功修改"+r+"条语句"); } } } private void Chaxun_Click(object sender, RoutedEventArgs e) { SqlHelper sh = new SqlHelper(); SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@pwd", "bbbbbb") }; string sql = "SELECT * FROM user1 WHERE [email protected]"; ArrayList DataList = sh.DataList(sql, pars); foreach(ArrayList list in DataList) { foreach (Object obj in list) { MessageBox.Show(obj.ToString()); } } } private void Chaxun2_Click(object sender,RoutedEventArgs e) { SqlHelper sh = new SqlHelper(); SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@pwd","bbbbbb") }; string sql = "SELECT * FROM user1 WHERE [email protected]"; ArrayList list = sh.DataSet(sql,pars); foreach(DataRow rows in list) { MessageBox.Show(rows["name"].ToString()); } } } }
3、SqlHelper.cs ado.net封装类
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Collections; using System.Data; namespace Test { //操作数据库方法 class SqlHelper { public string MyConnString { get { return @"Data source=LENOVO-PC;Initial Catalog=test;Integrated Security=False;User ID=sa;Password=root"; } } //执行insert,update,delete命令 //返回受影响的行数 public int ExcuteSQLReturnInt(string sql,SqlParameter[] pars) { SqlConnection conn = new SqlConnection(MyConnString); try { SqlCommand cmd = new SqlCommand(sql, conn); if (conn.State == System.Data.ConnectionState.Closed || conn.State == System.Data.ConnectionState.Broken) { conn.Open(); } if(pars!=null && pars.Length > 0) { foreach (SqlParameter p in pars) { cmd.Parameters.Add(p); } } int count = cmd.ExecuteNonQuery(); return count; } catch (Exception ex) { return 0; } finally { conn.Close(); } } //查询select public ArrayList DataList(string sql,SqlParameter[] pars) { SqlConnection conn = new SqlConnection(MyConnString); SqlCommand cmd = new SqlCommand(sql,conn); if (conn.State == System.Data.ConnectionState.Closed || conn.State == System.Data.ConnectionState.Broken) { conn.Open(); } if (pars != null && pars.Length > 0) { foreach (SqlParameter p in pars) { cmd.Parameters.Add(p); } } SqlDataReader reader = cmd.ExecuteReader(); ArrayList list = new ArrayList(); while (reader.Read()) { ArrayList list1 = new ArrayList(); for (int i = 0; i< reader.FieldCount; i++) { list1.Add(reader[i]); } list.Add(list1); } conn.Close(); return list; } //查询select public ArrayList DataSet(string sql,SqlParameter[] pars) { SqlConnection conn = new SqlConnection(MyConnString); SqlCommand cmd = new SqlCommand(sql,conn); if (conn.State == System.Data.ConnectionState.Closed || conn.State == System.Data.ConnectionState.Broken) { conn.Open(); } if (pars != null && pars.Length > 0) { foreach (SqlParameter p in pars) { cmd.Parameters.Add(p); } } SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); DataTable table = dataset.Tables[0]; DataRowCollection rows = table.Rows; ArrayList list = new ArrayList(); for(int i = 0; i < rows.Count; i++) { DataRow row = rows[i]; list.Add(row); } return list; } } }
时间: 2024-10-26 20:15:48