!!!SqlHelper 传智!
DataSet离线数据集
一般查询调用就用DataSet 将SQLserver数据库查询数据储存在vs操作内存中随时用随时遍历 以防做项目时突然与数据库断开连接,操作受限数据丢失带来的麻烦 若数据确实很大我们则用SqlDataReader dr=cmd.ExcuteReader() using (SqlConnection conn = new SqlConnection(@"server=PC-20150619EMUE\asb;Database=test;user=ewrw;pwd=463828")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select*from youdao where pwd=@pwd"; cmd.Parameters.Add(new SqlParameter("@pwd", txt.Text)); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); DataTable table = dataset.Tables[0]; DataRowCollection rows = table.Rows; for (int i = 0; i < rows.Count; i++) { DataRow row = rows[i]; int Age = (int)row["age"]; string name = (string)row["name"]; } } }
SqlHelper类建立绑定数据库 增删改查
步骤:
一,右键项目--新建项--添加App.config应用配置文件 以便项目以后客户联接别的服务器数据库好更改
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="dbConnstr" connectionString="server=.;Database=test;user=sa;pwd=32"/> </connectionStrings> </configuration>
二,右键引用 --添加引用 System.Configuration
三,
/// <summary> /// 使用长度可变参数来简化 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace WpfApplication2
{
class SqlHelper
{//调用APP.config配置文件中客户需要连接的数据库字符串
private static string connStr = ConfigurationManager.ConnectionStrings["dbconnstr"].ConnectionString;
//封装数据的原则 把不变的写到方法里,把变化的放到参数中
// 增删改的操作
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))//建立数据库联接 using释放空间
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())//创建操作数据库的命令
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();//执行操作的命令
}
}
}
// 查询返回一条结果的操作 如Count(*) ,sum()等..
public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters); // foreach(SqlParameter param in parameters)
//{
// cmd.Parameters.Add(param);
// }
return cmd.ExecuteScalar();
}
}
}
// DataSet离线查询结果返回表结构操作: 一般项目操作里面只有一个表所以直接返回一个表更方便
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
return dataset.Tables[0];
}
}
}
//DateSet 完整封装方式:
//public static DataSet ExecuteDataSet(string sql,params SqlParameter[] parameters)
//{using (SqlConnection conn=new SqlConnection(connStr))
// {
// conn.Open();
// using(SqlCommand cmd=conn.CreateCommand())
// {
// cmd.CommandText=sql;
// cmd.Parameters.AddRange(parameters);
// SqlDataAdapter adapter=new SqlDataAdapter(cmd);
// DataSet dataset=new DataSet();
// adapter.Fill(dataset);
// return dataset;
// }
// }
//}
////ExcuteReader 大数据联接数据库查询
//public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] parameters)
//{
// using (SqlConnection conn = new SqlConnection(connStr))
// {
// conn.Open();
// using (SqlCommand cmd = conn.CreateCommand())
// {
// cmd.CommandText = sql;
// cmd.Parameters.AddRange(parameters);
// return cmd.ExecuteReader();
// }
// }
//}
}
}
主页面后台 如何调用SqlHelper类??
private void Button_Click_2(object sender, RoutedEventArgs e) { SqlHelper.ExecuteNonQuery("insert into youdao(,,) values(@user,@pwd)",new SqlParameter[]{new SqlParameter("@user",500),new SqlParameter("@pwd","cdascas")}); DataTable table=SqlHelper.ExecuteDataTable("select*from youdao where user=@user,pwd=@pwd", new SqlParameter[] { new SqlParameter("@user", 500), new SqlParameter("@pwd", "cdascas") });
/*..............................................................................................................................*/
//长度可变参数来简化后调用 不用传数组new SqlParameters[],只传对象即可 DataTable table = SqlHelper.ExecuteDataTable("select*from youdao where user=@user,pwd=@pwd",new SqlParameter("@user", 500), new SqlParameter("@pwd", "cdascas") ); foreach(DataRow row in table.Rows) { string user = (string)row["user"]; }
DataSet dataset = SqlHelper.ExecuteDataSet("select*from youdao where user=@user,pwd=@pwd", new SqlParameter[] { new SqlParameter("@user", 500), new SqlParameter("@pwd", "cdascas") }); foreach (DataRow row in table.Tables[0].Rows) { string user = (string)row["user"]; } }
内容参要:http://net.itheima.com
http://net.itcast.cn