数据库连接(sqlconnection类)
Sqlconnection con = new Sqlconnection(ConfigurationManager.AppSettings["conStr"]);
注:conStr为web.config中配置的数据库连接字符串;
<appSettings>
<add key="conStr" Value="Server=(local);database=数据库名;Uid=sa;Pwd=sa;" />
</appSettings>
打开数据库连接(sqlconnection类和sqlcommand类)
Con.open();
String sqltext="SQL语句";
Sqlcommand cmd = new Sqlcommand(sqltext,con);
Cmd.connection.open();
关闭数据库连接(sqlconnection类和sqlcommand类)
Con.close();
Cmd.connection.close();
Sqlcommand类方法:
cmd.ExecuteNonQuery();
对连接执行SQL语句并返回受影响的行数;
cmd.ExecuteScalar();
执行查询,并返回查询所返回的结果集中第一行的第一列,忽略其它列和行;
NET下MD5加密技术
导入using System.Web.Security命名空间(默认已经导入);
加密:FormsAuthentication.HashPasswordForStoringInConfigFile("加密字符串","MD5");
NET下图像验证码技术
导入如下命名空间:
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
程序代码:
Protected void Page_Load(object sender,EventArgs e)
{
String strNum = RandNum(4);
Session["checkCode"]=strNum;
ValidateCode(strNum);
ViewState["checkCode"]=strNum;
}
/// <summary>
/// 产生随机数
/// </summary>
/// <param name="iNum">种子</param>
/// <returns></returns>
/// <author>zjl0135</author>
/// <history>2007-06-09</history>
private string RandNum(int iNum)
{
String strChar = "0,1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strAry = strChar.Split(',');
string strNum = "";
System.Random obj = new Random();
for (int i = 0; i < iNum; i++)
{
int index = obj.Next(strAry.Length);
strNum = strNum + strAry[index];
}
return strNum;
//这里可以对产生的随机数,进行加密处理。
}
/// <summary>
/// 重画图像生成
/// </summary>
/// <param name="strNum">验证码</param>
/// <author>zjl0135</author>
/// <history>2007-06-09</history>
private void ValidateCode(string strNum)
{
System.Drawing.Bitmap img = null;
System.Drawing.Graphics g = null;
System.IO.MemoryStream ms = null;
int iWidth = Convert.ToInt16(strNum.Length * 11.5);
img = new Bitmap(iWidth, 18);
g = Graphics.FromImage(img);
g.DrawString(strNum, (new Font("Arial", 8)), (new SolidBrush(Color.Blue)), 3, 3);
ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
img.Dispose();
Response.End();
}
Exception类:表示在应用程序执行时间产生的异常。
执行SQL语句函数
#region
///<summary>
/// 说明:execSQL用来执行SQL语句
/// 返回值:执行是否成功(true/false)
/// 参数:sqlText---SQL字符串
///</summary>
public Boolean execSQL(string sqlText)
{
SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
con.Open();
SqlCommand cmd=new SqlCommand(sqlText,con);
try
{
cmd.ExecuteNonQuery();//返回受影响的行数
con.Close();
}
catch(System.Exception e)
{
con.Close();
return false;//执行失败返回false
}
return true;//执行成功返回true,根据返回值正行判断操作
}
#endregion
返回执行语句的数据集
#region
///<summary>
/// 说明:返回数据源的数据集
/// 返回值:数据集DataSet
/// 参数:sqlText---SQL语句字符串,tableName---数据表名称
///</summary>
public System.Data.DataSet getDateSet(string sqlText,string tableName)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
con.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter(sqlText,con);
DataSet dataset = new DataSet();
dbAdapter.Fill(dataset,tableName);
con.Close();
return dataset;
}
#endregion
各字段单独访问方法(相对于ASP中的RS集合访问类似)
string sqlText="select * from tbNews";
DataSet ds = bc.getDateSet(sqlText,"tbNews");
DataRow[] row = ds.Tables[0].Select();
foreach (DataRow rs in row)
{
TextBox1.Text = rs["title"].ToString();
TextBox2.Text = rs["content"].ToString();
strStyle = rs["style"].ToString();
strType = rs["type"].ToString();
}
GridView控件操作相关
绑定数据
sqlText = "select * from tbNews";
GridView1.DataSource = bc.getDateSet(sqlText,"tbNews");
GridView1.DataKeyNames = new string[] { "id" };//给datakeynames赋值id关键字,会自动添加到datakeys集合中,方便后面使用
GridView1.DataBind();
删除操作(注意修改GridViewDeleteEventArgs e)
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string sqlText = "delete from tbNews where id='"+id+"'";
bc.execSQL(sqlText);
sqlText = "select * from tbNews";
GridView1.DataSource = bc.getDateSet(sqlText,"tbNews");
GridView1.DataBind();
}