• winform —— 连接数据库SQL Server 2008


    using System.Data.SqlClient;命名空间
    sqlconnection:数据连接类
    sqlcommand:数据库操作类
    sqldatareader:读取

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Data.SqlClient;
      6 using System.Drawing;
      7 using System.Linq;
      8 using System.Text;
      9 using System.Windows.Forms;
     10 
     11 namespace WindowsFormsApplication7
     12 {
     13     public partial class Form1 : Form
     14     {
     15         public Form1()
     16         {
     17             InitializeComponent();
     18         }
     19 
     20         private void button1_Click(object sender, EventArgs e)
     21         {
     22             string username = textBox1.Text;
     23             string upass = textBox2.Text;
     24 
     25             //第一步:连接到数据库
     26             SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
     27             conn.Open();
     28             //第二步:写执行语句
     29             SqlCommand cmd = conn.CreateCommand();//通过conn创建sqlcommand对象
     30             cmd.CommandText = "select *from users where uname='"+username+"' and upass='"+upass+"'";
     31             SqlDataReader dr = cmd.ExecuteReader();//执行查询,返回sqldatareader对象
     32             if (dr.Read())
     33             {
     34                 MessageBox.Show("登陆成功!");
     35             }
     36             else
     37             {
     38                 MessageBox.Show("登陆失败!");
     39             }
     40 
     41             conn.Close();
     42         }
     43 
     44         private void button2_Click(object sender, EventArgs e)
     45         {
     46             //添加数据到数据库
     47             string username = textBox1.Text;
     48             string upass = textBox2.Text;
     49 
     50             //第一步:连接到数据库
     51             SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
     52             conn.Open();
     53             SqlCommand cmd = conn.CreateCommand();
     54             cmd.CommandText = "insert into users values('"+username+"','"+upass+"')";
     55             int count= cmd.ExecuteNonQuery();//增删改
     56             if (count > 0)
     57             {
     58                 MessageBox.Show("添加成功!");
     59             }
     60             else
     61             {
     62                 MessageBox.Show("添加失败!");
     63             }
     64             conn.Close();
     65         }
     66 
     67         private void button3_Click(object sender, EventArgs e)
     68         {
     69             string code = textBox3.Text;
     70             string uname = textBox1.Text;
     71             string upass = textBox2.Text;
     72             //第一步:连接到数据库
     73             SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
     74             conn.Open();
     75             SqlCommand cmd = conn.CreateCommand();
     76             cmd.CommandText = "update users set uname='"+uname+"',upass='"+upass+"' where code="+code;
     77             int count=cmd.ExecuteNonQuery();
     78             if (count > 0)
     79             {
     80                 MessageBox.Show("修改成功!");
     81             }
     82             else
     83             {
     84                 MessageBox.Show("修改失败!");
     85             }
     86             conn.Close();
     87         }
     88 
     89         private void button4_Click(object sender, EventArgs e)
     90         {
     91             string code = textBox3.Text;
     92             SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
     93             conn.Open();
     94             SqlCommand cmd = conn.CreateCommand();
     95             cmd.CommandText = "delete from users where code="+code;
     96             cmd.ExecuteNonQuery();
     97             conn.Close();
     98         }
     99 
    100         private void Form1_Load(object sender, EventArgs e)
    101         {
    102 
    103         }
    104     }
    105 }
    View Code
  • 相关阅读:
    提高samba的传输速度[转载]
    centos安装samba,与其它OS共享文件[转载]
    解决*unix系统下Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    java在windows cmd中乱码
    mysql 主从复制配置
    mysql 开启binlog
    关于mysql varchar类型的长度
    MyISAM的表锁
    html页面跳转不保存上个页面的记录
    golang 下载所有依赖
  • 原文地址:https://www.cnblogs.com/viven/p/4439921.html
Copyright © 2020-2023  润新知