c#資料庫調用類

來源:互聯網
上載者:User
Web.config檔案: <appSettings>
<add key="connectionString" value="server=WAYGOING-12345;uid=sa;pwd=111111; database=111"/>
</appSettings> 類檔案://conn

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace datamtest
{
/// <summary>
/// conn 的摘要說明。
/// </summary>
public class conn
{
public conn()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
public SqlConnection connstr; //連接字串
public string getconnstr() //擷取連接字串
{
string constr;
constr=System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
return constr;
}
public void open() //開啟資料庫
{
string constr;
constr=getconnstr();
connstr=new SqlConnection(constr);
connstr.Open();
}
public void close() //關閉資料庫
{
connstr.Dispose();
connstr.Close();
}
public void execsql(string sql) //執行不帶傳回值的sql語句,如添加、修改、刪除
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
cmd.ExecuteNonQuery();
close();
}
public DataSet dataset(string sql) //返回DataSet對象
{
open();
SqlDataAdapter rs=new SqlDataAdapter(sql,connstr);
DataSet ds=new DataSet();
rs.Fill(ds,"ds");
return ds;
}
public DataView dataview(string sql) //返回DataView對象
{
DataSet ds=new DataSet();
ds=dataset(sql);
DataView dv=new DataView(ds.Tables[0]);
return dv;
}
public SqlDataReader datareader(string sql) //返回DataReader對象
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}

}
}

調用檔案:

//aspx檔案

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace datamtest
{
/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Button Button3;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面

if(!IsPostBack)
{
bind();
}
}
private void bind()
{
conn myconn = new conn();
// 調用dataset函數
DataSet ds=new DataSet();
string sql = "select * from admininfo";
ds = myconn.dataset(sql);
DataGrid1.DataSource=ds.Tables["ds"].DefaultView;
DataGrid1.DataBind();
myconn.close();
//調用DataReader函數
Label1.Text="";
SqlDataReader dr = myconn.datareader(sql);
while(dr.Read())
{
Label1.Text += dr["adminId"] + " " + dr["adminName"] + " " + dr["PassWord"] + " " + dr["LastLogin"] + " " + dr["LastLoginIp"] + "<br>";
}
myconn.close();
}

#region Web Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "insert into admininfo (adminId,adminName,PassWord,LastLogin,LastLoginIp) values ('"+TextBox5.Text+"','"+TextBox4.Text+"','"+TextBox3.Text+"','2007-1-1','192.168.0.1')";
myconn.execsql(sql);
bind();
}

private void Button2_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "update admininfo set PassWord = '" + TextBox1.Text + "' where id = " + TextBox2.Text;
myconn.execsql(sql);
bind();
}

private void Button3_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "delete admininfo where id = " + TextBox6.Text;
myconn.execsql(sql);
bind();
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.