winform —— 连接数据库SQL Server 2008
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的更多相关文章
- 使用变量向SQL Server 2008中插入数据
QT通过ODBC连接数据库SQL Server 2008,进行数据插入时遇到的问题: 先把数据存入变量中,如何使用变量进行插入?插入语句该怎么写? QSqlQuery query(db); query ...
- SQL Server 2008 R2安装图解教程
一.下载SQL Server 2008 R2安装文件 cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso 二.将安装文件刻录成光盘 ...
- 有关eclipse连接SQL Server 2008的问题
1.首先,提供一个链接http://blog.163.com/jackie_howe/blog/static/19949134720122261121214/ 这个链接有详细更改SQL Server ...
- sql server 2008 R2 配置开启远程访问
- SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)
本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...
- SQL Server 2008设置 开启远程连接
SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的, 如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置: 1,SQL Server ...
- SQL Server 2008 R2如何开启数据库的远程连接
SQL Server 2008 R2如何开启数据库的远程连接 SQL Server 2005以上版本默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远 ...
- Sql Server 2008开发版(Developer Edition)过期升级企业版(Enterprise Edition)失败后安装学习版(Express Edition)
最近一个多月,甚是悠哉,无事可做.上线的网站系统也没接到客户的反馈,反而觉得无聊之极了.上周五早上,一上QQ,就收到客户发来消息,管理平台无法登陆了.心里一惊,立马开始查找故障原因.翻看了系统日志,提 ...
- SQL Server 2008 开启数据库的远程连接
转载: 陈萌_1016----有道云笔记 SQL Server 2008默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远程连接数据库.需要做两个部 ...
随机推荐
- HTTP协议3之压缩--转
HTTP内容编码和HTTP压缩的区别 HTTP压缩,在HTTP协议中,其实是内容编码的一种. 在http协议中,可以对内容(也就是body部分)进行编码, 可以采用gzip这样的编码. 从而达到压缩的 ...
- 【问题备注】VS2012不能输入代码,文字…
第一次遇到,非常奇怪,一个项目,VS2012能正常打开,但是不能输入代码. 对比分析发现,其他项目能正常work.在于一个问题就是,VS2012 右下角有个INS一直在转一直analyzing,而正常 ...
- 【css基础】文本对齐,水平对齐,垂直对齐
先说水平对齐,那首先想到的就是text-align了,text-align:left,text-align:center,text- align:right,代表的就是左对齐,居中对齐和右对齐,需要注 ...
- C#索引器:在集合或数组中取出某一个元素 举例 _【转】
Garmmar: [访问修饰符] 数据类型 this[参数列表] { get { 获取索引器的内容 } set { 设置索引器的内容 } } Eg: <span style="font ...
- struts2 注解方式
struts2扫描方法: 扫描其位于包的命名注解的类 “struts, struts2, action 或 actions“. 接着,扫描相匹配下列任一条件的文件: 实例了 com.opensymph ...
- Spark IDEA开发环境构建
本文档基于IEDA构建spark maven应用. date: 2016/8/1 author: wangxl 1.下载IDEA https://www.jetbrains.com/idea/ 2.安 ...
- Linux脚本中使用特定JDK
有时linux系统中装了很多应用,我们又不能覆盖系统中设置的版本,此时我们就需要在脚本文件中设置特定版本. export JAVA_HOME= export CLASSPATH=.:$JAVA_HOM ...
- c中关于#与##的简易使用
#运算符用于在预编译时,将宏参数转换为字符串 eg. #include <stdio.h>#define CONVERT(f)(#f) void helloworld(){ printf( ...
- (原)Matlab的svmtrain和svmclassify
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5554551.html 参考网址: http://www.cnblogs.com/zhangchaoya ...
- javascript:自定义事件初探
javascript:自定义事件初探 http://www.cnblogs.com/jeffwongishandsome/archive/2008/10/27/1317148.html