C#数据库开发 实例教程及资源下载
答案:2 悬赏:60 手机版
解决时间 2021-02-07 19:47
- 提问者网友:你挡着我发光了
- 2021-02-07 14:54
C#数据库开发 实例教程及资源下载
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-02-07 15:42
------------------------------在web.config中
<connectionStrings>
<add name="Northwind" connectionString="Data Source=192.168.1.100;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
---------------------------在C#中调用:
String ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString
-----------------------------增删改查方法:
public int New(Entities.Ammeter obj)
{
String sql = "insert into Ammeter(AmmeterName,AmmeterNO,AmmeterTypeObjID,ApartmentObjID,BHNO,IsUsed,Multiplicator,ObjectID,ParentID,RealtyObjID,System) values(@AmmeterName,@AmmeterNO,@AmmeterTypeObjID,@ApartmentObjID,@BHNO,@IsUsed,@Multiplicator,@ObjectID,@ParentID,@RealtyObjID,@System)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@AmmeterName", obj.AmmeterName);
cmd.Parameters.AddWithValue("@AmmeterNO", obj.AmmeterNO);
cmd.Parameters.AddWithValue("@AmmeterTypeObjID", obj.AmmeterTypeObjID);
cmd.Parameters.AddWithValue("@ApartmentObjID", obj.ApartmentObjID);
cmd.Parameters.AddWithValue("@BHNO", obj.BHNO);
cmd.Parameters.AddWithValue("@IsUsed", obj.IsUsed);
cmd.Parameters.AddWithValue("@Multiplicator", obj.Multiplicator);
cmd.Parameters.AddWithValue("@ObjectID",
String.Empty.Equals(obj.ObjectID) ? System.Guid.NewGuid().ToString() : obj.ObjectID);
cmd.Parameters.AddWithValue("@ParentID", obj.ParentID);
cmd.Parameters.AddWithValue("@RealtyObjID", obj.RealtyObjID);
cmd.Parameters.AddWithValue("@System", obj.System);
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.Ammeter obj)
{
String sql = "Update Ammeter Set AmmeterName=@AmmeterName,AmmeterNO=@AmmeterNO,AmmeterTypeObjID=@AmmeterTypeObjID,ApartmentObjID=@ApartmentObjID,BHNO=@BHNO,IsUsed=@IsUsed,Multiplicator=@Multiplicator,ParentID=@ParentID,RealtyObjID=@RealtyObjID,System=@System Where ObjectID=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@AmmeterName", obj.AmmeterName);
cmd.Parameters.AddWithValue("@AmmeterNO", obj.AmmeterNO);
cmd.Parameters.AddWithValue("@AmmeterTypeObjID", obj.AmmeterTypeObjID);
cmd.Parameters.AddWithValue("@ApartmentObjID", obj.ApartmentObjID);
cmd.Parameters.AddWithValue("@BHNO", obj.BHNO);
cmd.Parameters.AddWithValue("@IsUsed", obj.IsUsed);
cmd.Parameters.AddWithValue("@Multiplicator", obj.Multiplicator);
cmd.Parameters.AddWithValue("@ObjectID", obj.ObjectID);
cmd.Parameters.AddWithValue("@ParentID", obj.ParentID);
cmd.Parameters.AddWithValue("@RealtyObjID", obj.RealtyObjID);
cmd.Parameters.AddWithValue("@System", obj.System);
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.Ammeter obj)
{
String sql = "delete from Ammeter Where ObjectID=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.ObjectID);
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 Ammeter 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 pkField, Object pkid)
{
String sql = String.Format("select * from Ammeter Where {0}='{1}'", pkField.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""), pkid.ToString().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;
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from Ammeter 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;
}
}
--------------------------------------------------电子书我有不少,需要的话传给你
<connectionStrings>
<add name="Northwind" connectionString="Data Source=192.168.1.100;Initial Catalog=Northwind;User ID=sa;PassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
---------------------------在C#中调用:
String ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString
-----------------------------增删改查方法:
public int New(Entities.Ammeter obj)
{
String sql = "insert into Ammeter(AmmeterName,AmmeterNO,AmmeterTypeObjID,ApartmentObjID,BHNO,IsUsed,Multiplicator,ObjectID,ParentID,RealtyObjID,System) values(@AmmeterName,@AmmeterNO,@AmmeterTypeObjID,@ApartmentObjID,@BHNO,@IsUsed,@Multiplicator,@ObjectID,@ParentID,@RealtyObjID,@System)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@AmmeterName", obj.AmmeterName);
cmd.Parameters.AddWithValue("@AmmeterNO", obj.AmmeterNO);
cmd.Parameters.AddWithValue("@AmmeterTypeObjID", obj.AmmeterTypeObjID);
cmd.Parameters.AddWithValue("@ApartmentObjID", obj.ApartmentObjID);
cmd.Parameters.AddWithValue("@BHNO", obj.BHNO);
cmd.Parameters.AddWithValue("@IsUsed", obj.IsUsed);
cmd.Parameters.AddWithValue("@Multiplicator", obj.Multiplicator);
cmd.Parameters.AddWithValue("@ObjectID",
String.Empty.Equals(obj.ObjectID) ? System.Guid.NewGuid().ToString() : obj.ObjectID);
cmd.Parameters.AddWithValue("@ParentID", obj.ParentID);
cmd.Parameters.AddWithValue("@RealtyObjID", obj.RealtyObjID);
cmd.Parameters.AddWithValue("@System", obj.System);
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.Ammeter obj)
{
String sql = "Update Ammeter Set AmmeterName=@AmmeterName,AmmeterNO=@AmmeterNO,AmmeterTypeObjID=@AmmeterTypeObjID,ApartmentObjID=@ApartmentObjID,BHNO=@BHNO,IsUsed=@IsUsed,Multiplicator=@Multiplicator,ParentID=@ParentID,RealtyObjID=@RealtyObjID,System=@System Where ObjectID=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@AmmeterName", obj.AmmeterName);
cmd.Parameters.AddWithValue("@AmmeterNO", obj.AmmeterNO);
cmd.Parameters.AddWithValue("@AmmeterTypeObjID", obj.AmmeterTypeObjID);
cmd.Parameters.AddWithValue("@ApartmentObjID", obj.ApartmentObjID);
cmd.Parameters.AddWithValue("@BHNO", obj.BHNO);
cmd.Parameters.AddWithValue("@IsUsed", obj.IsUsed);
cmd.Parameters.AddWithValue("@Multiplicator", obj.Multiplicator);
cmd.Parameters.AddWithValue("@ObjectID", obj.ObjectID);
cmd.Parameters.AddWithValue("@ParentID", obj.ParentID);
cmd.Parameters.AddWithValue("@RealtyObjID", obj.RealtyObjID);
cmd.Parameters.AddWithValue("@System", obj.System);
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.Ammeter obj)
{
String sql = "delete from Ammeter Where ObjectID=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.ObjectID);
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 Ammeter 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 pkField, Object pkid)
{
String sql = String.Format("select * from Ammeter Where {0}='{1}'", pkField.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""), pkid.ToString().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;
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from Ammeter 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;
}
}
--------------------------------------------------电子书我有不少,需要的话传给你
全部回答
- 1楼网友:梦中风几里
- 2021-02-07 16:52
using system.data.oledb;//你没有添加这个private void loginbutton_click(object sender, eventargs e) { if (gonghaotextbox.text.trim() == "" || mimatextbox.text.trim() == "") { messagebox.show("请输入工号或者密码!", "错误提示"); } else { string gonghao = this.gonghaotextbox.text.trim(); string mima = this.mimatextbox.text.trim(); bool flag1 = false; try { //sqlconnection conn = new sqlconnection("data source=pc-20150105jsgs;persist security info=true;initial catalog=yuangonglogin;user id=sa;password=sql"); string conn = "data source=gd-sc-gcb-001;persist security info=true;initial catalog=工资管理系统;user id=sa;password=sql"; sqlconnection sqlcnt = new sqlconnection(conn); // sqlcnt.connectionstring = configurationmanager.connectionstrings["strcnt"].connectionstring; sqlcnt.open(); sqlcommand scomm = new sqlcommand();//初始化sql命令对象 scomm.commandtext = "select *
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯