WPF操作SQL SERVER 数据库实例讲解,wpf实例讲解
WPF操作SQL SERVER 数据库实例讲解
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;
namespace demo_sql
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
private void add_Click(object sender, RoutedEventArgs e)
{
string strconn = "server=(localdb)\\MSSQLLocalDB;database=studentdb;integrated security=true";
SqlConnection sqlconn = new SqlConnection(strconn);
try
{
sqlconn.Open();
//MessageBox.Show("连接数据库成功");
string sqladd = "insert into student(name, password) values ('" +name.Text + "', '" + password.Text + "')";
SqlCommand sqlcmd = new SqlCommand(sqladd, sqlconn);
sqlcmd.ExecuteNonQuery();
MessageBox.Show("插入成功");
}
catch (Exception ex)
{
MessageBox.Show("数据库打开失败,详细信息:" + ex.ToString());
}
finally
{
sqlconn.Close();
}
}
private void modify_Click(object sender, RoutedEventArgs e)
{
string strconn = "server=(localdb)\\MSSQLLocalDB;database=studentdb;integrated security=true";
SqlConnection sqlconn = new SqlConnection(strconn);
try
{
sqlconn.Open();
string strmodify = "Update student set password='" + password.Text + "'" + " where name=" + "'" + name.Text + "'";
SqlCommand sqlcmd = new SqlCommand(strmodify, sqlconn);
sqlcmd.ExecuteNonQuery();
MessageBox.Show("修改成功");
}
catch (Exception ex)
{
MessageBox.Show("连接错误" + ex.Message);
}
finally
{
sqlconn.Close();
}
}
private void delete_Click(object sender, RoutedEventArgs e)
{
string strconn = "server=(localdb)\\MSSQLLocalDB;database=studentdb;integrated security=true";
SqlConnection sqlconn = new SqlConnection(strconn);
try
{
sqlconn.Open();
string strdelete = "Delete from student where name='" + name.Text + "'";
SqlCommand sqlcmd = new SqlCommand(strdelete, sqlconn);
sqlcmd.ExecuteNonQuery();
MessageBox.Show("删除成功");
}
catch (Exception ex)
{
MessageBox.Show("连接错误"+ex.Message);
}
finally
{
sqlconn.Close();
}
}
private void search_Click(object sender, RoutedEventArgs e)
{
int flag = 1;
string strconn = "server=(localdb)\\MSSQLLocalDB;database=studentdb;integrated security=true";
SqlConnection sqlconn = new SqlConnection(strconn);
try
{
sqlconn.Open();
//MessageBox.Show("连接数据库成功");
string sqlsearch = "select * from student where name='" + name.Text + "'";
SqlCommand sqlcmd = new SqlCommand(sqlsearch, sqlconn);
SqlDataReader reader = sqlcmd.ExecuteReader();
//读取数据
while (reader.Read())
{
// 可以使用数据库中的字段名,也可以使用角标访问
if(reader["password"].ToString()==password.Text )
{
flag = 0;
break;
}
}
if(flag == 1)
MessageBox.Show("用户不存在");
else
MessageBox.Show("存在用户");
}
catch (Exception ex)
{
MessageBox.Show("数据库打开失败,详细信息:" + ex.ToString());
}
finally
{
sqlconn.Close();
}
}
}
}
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。