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

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string upass = textBox2.Text; //第一步:连接到数据库
SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
conn.Open();
//第二步:写执行语句
SqlCommand cmd = conn.CreateCommand();//通过conn创建sqlcommand对象
cmd.CommandText = "select *from users where uname='"+username+"' and upass='"+upass+"'";
SqlDataReader dr = cmd.ExecuteReader();//执行查询,返回sqldatareader对象
if (dr.Read())
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("登陆失败!");
} conn.Close();
} private void button2_Click(object sender, EventArgs e)
{
//添加数据到数据库
string username = textBox1.Text;
string upass = textBox2.Text; //第一步:连接到数据库
SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into users values('"+username+"','"+upass+"')";
int count= cmd.ExecuteNonQuery();//增删改
if (count > )
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
conn.Close();
} private void button3_Click(object sender, EventArgs e)
{
string code = textBox3.Text;
string uname = textBox1.Text;
string upass = textBox2.Text;
//第一步:连接到数据库
SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update users set uname='"+uname+"',upass='"+upass+"' where code="+code;
int count=cmd.ExecuteNonQuery();
if (count > )
{
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}
conn.Close();
} private void button4_Click(object sender, EventArgs e)
{
string code = textBox3.Text;
SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from users where code="+code;
cmd.ExecuteNonQuery();
conn.Close();
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}

winform —— 连接数据库SQL Server 2008的更多相关文章

  1. 使用变量向SQL Server 2008中插入数据

    QT通过ODBC连接数据库SQL Server 2008,进行数据插入时遇到的问题: 先把数据存入变量中,如何使用变量进行插入?插入语句该怎么写? QSqlQuery query(db); query ...

  2. SQL Server 2008 R2安装图解教程

    一.下载SQL Server 2008 R2安装文件 cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso 二.将安装文件刻录成光盘 ...

  3. 有关eclipse连接SQL Server 2008的问题

    1.首先,提供一个链接http://blog.163.com/jackie_howe/blog/static/19949134720122261121214/ 这个链接有详细更改SQL Server ...

  4. sql server 2008 R2 配置开启远程访问

  5. SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)

    本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...

  6. SQL Server 2008设置 开启远程连接

    SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的, 如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置: 1,SQL Server ...

  7. SQL Server 2008 R2如何开启数据库的远程连接

    SQL Server 2008 R2如何开启数据库的远程连接 SQL Server 2005以上版本默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远 ...

  8. Sql Server 2008开发版(Developer Edition)过期升级企业版(Enterprise Edition)失败后安装学习版(Express Edition)

    最近一个多月,甚是悠哉,无事可做.上线的网站系统也没接到客户的反馈,反而觉得无聊之极了.上周五早上,一上QQ,就收到客户发来消息,管理平台无法登陆了.心里一惊,立马开始查找故障原因.翻看了系统日志,提 ...

  9. SQL Server 2008 开启数据库的远程连接

     转载: 陈萌_1016----有道云笔记 SQL Server 2008默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远程连接数据库.需要做两个部 ...

随机推荐

  1. Git 笔记三 Git的初步使用

    Git 笔记三 Git的初步使用 在上一篇中,学习了如何配置Git环境,这一篇,开始学习Git的初步使用.Git的初步使用还是很简单的.总体上知道git init, git clone, git ad ...

  2. C# Datatable导出Excel方法

    C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: public static bool ExportCSV(DataTable dt, string f ...

  3. js将对象转成字符串-支持微信

    最近写一个微信项目时用到了 把对象转成字符串,因为我需要把它存在cookie中,碰到了一些问题,在这里分享一下. 要转换的就是这货~ var FBinf = { "workPlacesCod ...

  4. Entity Framework数据库迁移

    1.启用数据迁移: enable-Migrations2.增加一条数据库迁移指令:add-Migrations 必须带上一个版本名称,比如AddUsernamePassword完整的指令:add-Mi ...

  5. PHP学习笔记二十四【Get Set】

    <?php Class Person{ private $n1; private $n2; private $n3; //使用__set方法来管理所有的属性 public function __ ...

  6. 改变navigationbar的底部线条颜色

    [[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; CG ...

  7. sketchup 导出 fbx文件 单位 错误

    最近在使用sketchup导出fbx文件到unity中使用时,发生了尺度单位上的错误.按照网上给出的标准教程,选定模型的单位为十进制-米.导出时选项选择'米',但是得到的fbx文件在unity中出现了 ...

  8. (转)Should I use char** argv or char* argv[]

      As you are just learning C, i recommend you to really try to understand the differences between ar ...

  9. 高可用集群(HA)之DRBD原理和基础配置

    目录 1.工作原理图 2.用户空间工具 3.工作模式 4.实现主备故障自动切换 5.所需软件 6.配置文件 7.详细配置     1.配置通用属性信息     2.定义一个资源     3.初始化资源 ...

  10. Python学习笔记(1)——数组差集

    面试的时候被问到这样一个问题:有A.B两个数组,找出B中有A中没有的所有元素(换言之即是求差集B-A).当时比较紧张,用了最原始的双重嵌套循环逐个比较,很显然这种时间复杂度高达O(n2)的算法相当lo ...