数据库连接及SQL语句操作
2005-06-08 13:22 张剑 阅读(1186) 评论(0) 编辑 收藏 举报1.Web.config文件设置
<appSettings>
<add key="DataSource" value="zj.mdb" />
<add key="Provider" value="Provider = Microsoft.Jet.OLEDB.4.0" />
</appSettings>
2.strConn数据库连接类
//数据库连接``
public static string strConn
{
get
{
StringBuilder strResult = new StringBuilder(ConfigurationSettings.AppSettings["Provider"]);
strResult.Append("; ");
strResult.Append("Data Source = ");
strResult.Append(HttpContext.Current.Server.MapPath("."));
strResult.Append("\\");
strResult.Append(ConfigurationSettings.AppSettings["DataSource"]);
return strResult.ToString();
}
}
3.SQL语句插入操作
strSQL = "INSERT INTO [gbook] (dname ) VALUES (@name )";
Conn.Open();
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@name", OleDbType.Char)).Value = dname;
Cmd.ExecuteNonQuery();
4.SQL语句更新操作
strSQL = "UPDATE [online] SET [Online_Name]=@Name WHERE Online_IP = @ip";
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@Name", OleDbType.Char)).Value = strName;
Cmd.Parameters.Add(new OleDbParameter("@ip", OleDbType.Char)).Value = strIP;
Cmd.ExecuteNonQuery();
5.SQL语句删除操作
strSQL = "DELETE FROM [online] WHERE Online_IP = @ip";
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@ip", OleDbType.Char)).Value = strIP;
Cmd.ExecuteNonQuery();
6.SQL语句查询操作
strSQL = "SELECT * FROM [topic] WHERE Online_IP = @ip";
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@ip", OleDbType.Char)).Value = strIP;
Cmd.ExecuteNonQuery();
7.SQL语句统计录数
strSQL = "SELECT COUNT(*) FROM reply WHERE Topic_ID = @id";
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@id", OleDbType.Integer)).Value = intID;
intTotalRecord = (int)Cmd.ExecuteScalar();