频道栏目
首页 > 数据库 > SQL Server > 正文
WPF操作SQL SERVER 数据库实例讲解
2018-07-21 14:17:16         来源:Liuqiang1997514的博客  
收藏   我要投稿

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();
        }
    }
}

}

点击复制链接 与好友分享!回本站首页
上一篇:数据库之SQL联合查询、子查询与嵌套子查询等实例
下一篇:sql语句查询时间的方式比较(代码实例)
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站