using System.Data.SqlClient;
using System.Data;
上面是必须的
下面说的都是用存储过程
首先是webconfig里面的连接字符串:
class="code_img_closed" src="/Upload/Images/2013120615/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('a5e1d090-40df-4186-b7cf-3f7b7d6ec709',event)" src="/Upload/Images/2013120615/2B1B950FA3DF188F.gif" alt="" /><connectionStrings> <add name="StatisticsConnectionString" connectionString="data source=.;Initial Catalog=Statistics;Integrated Security=true;" providerName="System.Data.SqlClient" /> </connectionStrings>Web.config
其次是自己写的最简单的连接数据库的类:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; namespace Mystatistics { /// <summary> /// 连接数据库的类 /// </summary> public class DBLink { public static SqlConnection GetConnection() { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["StatisticsConnectionString"].ConnectionString); return conn; } } }DBLink
然后是一个简单的绑定控件,其实这是从数据库里面获得了一个表,放到了datatable中
SqlConnection conn = DBLink.GetConnection(); //连接 conn.Open(); SqlCommand cmd = new SqlCommand(); //命令 cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; //指定命令类型是存储过程 cmd.CommandText = "procMajorsByinstituteIdSelect"; //存储过程名 cmd.Parameters.Add(new SqlParameter("@inInstituteId", instituteId)); //存储过程用到的参数,可以按这个一直添加 SqlDataAdapter sda = new SqlDataAdapter(cmd); //相当于内存里的datatable和从数据库获得的结果之间的桥梁 DataTable dt = new DataTable(); //这个表装从数据库获得的数据,相当于本机内存里面的一个表 sda.Fill(dt); //填充 /*从datatable绑定,并设置*/ ddlMajor.DataValueField = "majorId"; //值,这两个字符串来自数据库select语句 ddlMajor.DataTextField = "majorName"; //显示文字 ddlMajor.DataSource = dt; ddlMajor.DataBind(); //绑定控件 /*最后释放资源*/ dt.Dispose(); sda.Dispose(); cmd.Dispose(); //释放 conn.Close(); //关闭
下面是存储过程获得了多个表的处理,注意几点:1.dataset 2.dataset到datatable 3.datatable获得指定行列的值 4.select语句没有结果的处理
SqlConnection conn = DBLink.GetConnection(); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "procGetMonthReport"; //存储过程名 cmd.Parameters.Add(new SqlParameter("@startTime", startTime)); cmd.Parameters.Add(new SqlParameter("@endTime", endTime)); SqlDataAdapter sda= new SqlDataAdapter(cmd); DataSet ds = new DataSet(); //这个cmd 获得的结果是多个表,用dataset装 sda.Fill(ds); /*处理获得的数据,注意若select没有结果但是想显示为0的强制转换的话会出现异常*/ /*获得的第一个表*/ underStuSum = Convert.IsDBNull(ds.Tables[0].Rows[0]["unum"])?0:Convert.ToInt32(ds.Tables[0].Rows[0]["unum"]); mStuSum = Convert.IsDBNull(ds.Tables[0].Rows[0]["mnum"]) ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["mnum"]); dStuSum = Convert.IsDBNull(ds.Tables[0].Rows[0]["dnum"]) ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["dnum"]); mdStuSum = mStuSum + dStuSum; lblUnderStuSum.Text = underStuSum.ToString(); lblMDStuSum.Text = mdStuSum.ToString(); /*获得的第二个表*/ singleRecruitsSum = Convert.IsDBNull(ds.Tables[1].Rows[0]["singlecompanynum"]) ? 0 : Convert.ToInt32(ds.Tables[1].Rows[0]["singlecompanynum"]); lblSingleRecruitsSum.Text = singleRecruitsSum.ToString(); /*获得的第三个表*/ int u = Convert.IsDBNull(ds.Tables[2].Rows[0]["unum"]) ? 0 : Convert.ToInt32(ds.Tables[2].Rows[0]["unum"]); int m = Convert.IsDBNull(ds.Tables[2].Rows[0]["mnum"]) ? 0 : Convert.ToInt32(ds.Tables[2].Rows[0]["mnum"]); int d = Convert.IsDBNull(ds.Tables[2].Rows[0]["dnum"]) ? 0 : Convert.ToInt32(ds.Tables[2].Rows[0]["dnum"]); singleProvidesSum = u + m + d; lblSingleProvidesSum.Text = singleProvidesSum.ToString(); /*获得的第四个表*/ groupSum = Convert.IsDBNull(ds.Tables[3].Rows[0]["groupsum"]) ? 0 : Convert.ToInt32(ds.Tables[3].Rows[0]["groupsum"]); lblGroupSum.Text = groupSum.ToString(); /*获得的第五个表*/ groupCompanysSum = Convert.IsDBNull(ds.Tables[4].Rows[0]["groupcompanysum"]) ? 0 : Convert.ToInt32(ds.Tables[4].Rows[0]["groupcompanysum"]); lblGroupCompanysSum.Text = groupCompanysSum.ToString(); /*获得的第六个表*/ int gu = Convert.IsDBNull(ds.Tables[5].Rows[0]["unum"]) ? 0 : Convert.ToInt32(ds.Tables[5].Rows[0]["unum"]); int gm = Convert.IsDBNull(ds.Tables[5].Rows[0]["mnum"]) ? 0 : Convert.ToInt32(ds.Tables[5].Rows[0]["mnum"]); int gd = Convert.IsDBNull(ds.Tables[5].Rows[0]["dnum"]) ? 0 : Convert.ToInt32(ds.Tables[5].Rows[0]["dnum"]); groupProvidesSum = gu + gm + gd; lblGroupProvidesSum.Text = groupProvidesSum.ToString(); ds.Dispose(); sda.Dispose(); cmd.Dispose(); conn.Close();
下面是没有用存储过程的:
SqlConnection conn = DBLink.GetConnection(); conn.Open(); SqlCommand cmd = new SqlCommand("select TypeID,TypeName from ArticleType", conn); if (ArticleType.Items.Count != 0) ArticleType.Items.Clear(); SqlDataReader sdr = cmd.ExecuteReader(); ArticleType.DataSource = sdr; ArticleType.DataTextField = "TypeName"; ArticleType.DataValueField = "TypeID"; ArticleType.DataBind(); conn.Close(); ArticleType.Items.Insert(0, "请选择文章类型...");