asp.net的最簡單的增刪改查的例子
阿新 • • 發佈:2018-12-24
第一,
1.設定資料庫可以遠端訪問(以sqlserver為例)
開始選單---程式--- Microsoft sql server 2005---配置工具--sqlserver 外圍配置器
---服務和連線的外圍配置器
開啟 MSSqlServer---DataBase Engine--遠端連線
選擇本地連線和遠端連線,再選擇同時使用TCP/IP 和named pipes 確定
2.建立Winform 應用程式
3.新增應用程式配置檔案 App.config 檔案,在
<configuration>
</configuration>
之間加入
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
其中IP地址和資料庫名稱、使用者名稱和密碼需要改成你本地實際的
第二,
1.資料庫建表,
create table mytable(
pkid nvarchar(40) not null,
a nvarchar(40) null
)
insert into mytable(pkid,a) values(newid(),'測試')
2.新建對應此表的實體類
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _pkid; }
set { _pkid = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
}
3.新建操作類:
public class mytableDAO
{
private String _ConnectionString;
public mytableDAO(String ConnectionString)
{
this._ConnectionString = ConnectionString;
}
public int New(Entities.MsgRecord obj)
{
String sql = "insert into mytable(pkid,a) values(@pkid,@a)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set[email protected] Where [email protected]";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(Entities.mytable obj)
{
String sql = "delete from mytable Where[email protected]";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""));
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(this._ConnectionString));
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
}
4.在ui中呼叫:
4.1 新增:
private void NewData()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
mytable obj=new mytable();
obj.a=this.txtbox_a.Text;
obj.b=this.txtbox_b.Text;
obj.c=this.txtbox_c.Text;
dao.New(obj);
}
4.2 取資料繫結GridView
private void LoadData_InitGridView()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
Table dt=dao.Query(String.Format(" a ='{0}'",this.txtbox_a.Text));
//gridview
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
//textbox control
this.textbox_a.Text=dt.Rows[0][0].ToString();
}
4.3刪除
private void Del()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
//根據條件刪除指定記錄
dao.Del(where);
}
4.4更新
private void Update()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
dao.Update(obj);//obj 為mytable的一個例項:需要更新的
}
5.配置檔案中連線資訊
<connectionStrings>
<add name="dbCon" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
6.讀取連線資訊
System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString