首先介绍一下SqlConnection类
SqlConnection类表示一个到SQL Server数据库的打开的连接,此类不能被继承
SqlConnection类的构造函数有如下3个
SqlConnection:初始化 SqlConnection 类的新实例。
SqlConnection(String):如果给定包含连接字符串的字符串,则初始化 SqlConnection 类的新实例。
SqlConnection(String, SqlCredential):初始化给定连接字符串的 SqlConnection类的新实例,而不使用包含用户识别号和密码的 Integrated Security = true 和 SqlCredential 对象。
常用的方法:
Open 使用 ConnectionString 所指定的属性设置打开数据库连接
Close 关闭与数据库的连接,此方法是关闭任何已打开连接的首选方法
CreateCommand 创建并返回一个与SqlConnection关联的SqlCommand对象
Dispose 释放由Component使用的所有资源
SqlConnection对象若使用了带一个string类型参数的构造函数,这个参数叫做连接字符串
接下来介绍一下command
Command 常用属性
CommText 要下达至数据源的命令
commanTimeout 出错等待时间
Command 三种方法
ExecuteNonQuery() 不返回值 ,一般应用于insert,update,delete语句中
ExecuteScalar() 返回一个值,一般用于放回一个值的语句,如需求数据统计的count语句,求最大数Max语句等
ExcuteReader() 返回一个IDataReader,可以用于迭代返回记录
还有介绍一下SqlDataAdapter是DataSet
sqlDataAdapter是dataSet和数据库的连接(桥接器),用于检索和保存数据,SqlDataAdaoter通过对数据源使用适当的Transact-SQL语句映射File(他作为填充DataSet的数据源中的数据)和Update(更改DataSet中的数据源)提供一种桥接方式,当SqlDataAdapter填充DataSet时,他为返回的数据创建必须的表和列
第一种方式(字符串查询)
string strCon =“uid=sa;pwd=123456;database=test;server=127.0.0.1”;
sql = “select * from table”;
SqlDataAdapter da = new SqlDataAdapter(sql,strCon);
DataSet ds = new DataSet();
da.Fill(ds,“临时虚拟表”);
dataGridView1.DataSource= ds.Tables[“临时虚拟表”];
第二种方式使用SqlConneclion对象来创建
string strConn=“uid=账号;pwd=密码;database=数据库;server=服务器”;//SQL Server链接字符串
SqlConnection conn=new SqlConnection(strConn);
string strSql=“SELECT * FROM 表名”;
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds=new DataSet();//创建DataSet实例
da.Fill(ds,“自定义虚拟表名”);//使用DataAdapter的Fill方法(填充),调用SELECT命令
第三种方式通过SqlCommand对象
string strConn=“uid=账号;pwd=密码;database=数据库;server=服务器”;//SQL Server链接字符串
SqlConnection connSql=new SqlConnection (strConn); //Sql链接类的实例化
connSql.Open ();//打开数据库
//使用SqlDataAdapter时没有必要从Connection.open()打开,
//SqlDataAdapter会自动打开关闭它。
string strSql = “SELECT * FROM 表名”; //要执行的SQL语句
SqlCommand cmd = new SqlCommand(strSql, connSql);
SqlDataAdapter da=new SqlDataAdapter(cmd); //创建DataAdapter数据适配器实例
DataSet ds=new DataSet();//创建DataSet实例
da.Fill(ds,“自定义虚拟表名”);//使用DataAdapter的Fill方法(填充),调用SELECT命令
connSql.Close();//关闭数据库
如果只需要执行SQL语句或SP,就没必要用到DataAdapter ,直接用SqlCommand的Execute系列方法就可以了。SqlDataadapter的作用是实现Dataset和DB之间的桥梁:比如将对DataSet的修改更新到数据库。
SqlDataAdapter的UpdateCommand的执行机制是:当调用SqlDataAdapter.Update()时,检查DataSet中的所有行,然后对每一个修改过的Row执行SqlDataAdapter.UpdateCommand ,也就是说如果未修改DataSet中的数据,SqlDataAdapter.UpdateCommand不会执行。
1、在使用Fill方式时,可以指定DataTable,而不是DataSet:
string strConn=“uid=账号;pwd=密码;database=数据库;server=服务器”;//SQL Server链接字符串
strSql=“SELECT * FROM 表名”;
SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);
DataTable tbl=new DataTable( );
da.Fill(tbl);
注意打开和关闭连接的处理
这是winform控件的布局
登录界面的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dlu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string username = "";
public string userpassword = "";
private void Form1_Load(object sender, EventArgs e)
{
}
//用户进行登录 NewMethod
private void NewMethod()
{
if(this.textBox1.Text.Trim().Equals("") || this.textBox2.Text.Trim().Equals(""))
{
MessageBox.Show("用户名或者密码不能为空");
return;
}
else
{
//到数据库中验证
//database是数据库的库名
//uid是数据库登录的登录名
//pwd是数据库登录的密码
SqlConnection connection = new SqlConnection("server=.;database=;uid=;pwd=;");
connection.Open(); //建立sql连接
SqlCommand com = new SqlCommand();
com.Connection = connection;
com.CommandType = CommandType.Text;
com.CommandText = "select * from [dbo].[dlu] where username='" + this.textBox1.Text + "' and userpassword='" + this.textBox2.Text + "'";
SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); //执行sql语句
//CommandBehavior.CloseConnection 断开连接
if(dr.Read())
{
username = textBox1.Text;
userpassword = textBox2.Text;
MessageBox.Show("登录成功", "登录提示", MessageBoxButtons.OK);
Form3 form3 = new Form3();
form3.Show();
}
else
{
//信息错误 判断条件不成立
MessageBox.Show("密码错误", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
NewMethod();
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否要进行注册?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
}
}
注册界面的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dlu
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//用户进行注册 enroll
private void enroll()
{
//到数据库中验证
//database是数据库的库名
//uid是数据库登录的登录名
//pwd是数据库登录的密码
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "server=.;database=;uid=;pwd=;";
string strcmd = "insert into [dbo].[dlu] values('" + textBox1.Text + "','" + textBox2.Text + "')"; //执行sql语句 在数据库插入用户的注册信息
SqlCommand mycommand = new SqlCommand(strcmd, connection);
try
{
connection.Open();
mycommand.ExecuteNonQuery();
MessageBox.Show("注册成功!");
}
catch
{
MessageBox.Show("注册失败!");
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
enroll();
}
}
}