App_Code文件夹中对数据库的操作该如何写代码(asp.net+sql 2000+c#) 10
我知道App_Code文件夹中的代码对数据库的操作,在某些页面上调用数据库时只要调用App_Code文件夹中代码就可以了.但是对数据库操作的代码该如何去写呢,请高手们赐教...
我知道App_Code文件夹中的代码对数据库的操作,在某些页面上调用数据库时只要调用App_Code文件夹中代码就可以了.但是对数据库操作的代码该如何去写呢,请高手们赐教,谢谢!
展开
3个回答
展开全部
其实就是写一个连接类,然后调用就可以了;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
/// <summary>
/// DBConnection 的摘要说明
/// </summary>
public class DBconnection
{
SqlConnection conn = null;
SqlCommand cmd = new SqlCommand();
public DBconnection()
{
conn = new SqlConnection("server=.;database=university_db;uid=sa;pwd=123;");
}
public SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
conn.Open();//打开连接
return conn;
}
//关闭连接
public void clear()
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
然后在你要用连接的页面的.cs页面调用就可以了呀
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
/// <summary>
/// DBConnection 的摘要说明
/// </summary>
public class DBconnection
{
SqlConnection conn = null;
SqlCommand cmd = new SqlCommand();
public DBconnection()
{
conn = new SqlConnection("server=.;database=university_db;uid=sa;pwd=123;");
}
public SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
conn.Open();//打开连接
return conn;
}
//关闭连接
public void clear()
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
然后在你要用连接的页面的.cs页面调用就可以了呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上面的问题就是你的答案,太巧了
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = System.Configuration.ConfigurationManager.ConnectionStrings["loginConnectionString"].ToString();
SqlConnection con = new SqlConnection(s);
con.Open();
string name = this.TextBox1.Text.Trim();
string pwd = this.TextBox2.Text.Trim();
string sql = "insert into denglu(name,pwd)values('"+name+"','"+pwd+"')";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!');</script>");
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = System.Configuration.ConfigurationManager.ConnectionStrings["loginConnectionString"].ToString();
SqlConnection con = new SqlConnection(s);
con.Open();
string name = this.TextBox1.Text.Trim();
string pwd = this.TextBox2.Text.Trim();
string sql = "insert into denglu(name,pwd)values('"+name+"','"+pwd+"')";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!');</script>");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
从问题可以看出你是刚入门的
花些时间看入门教程去
花些时间看入门教程去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询