永发信息网

C#数据库开发 实例教程及资源下载

答案:2  悬赏:60  手机版
解决时间 2021-02-07 19:47
C#数据库开发 实例教程及资源下载
最佳答案
------------------------------在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;
}
}
--------------------------------------------------电子书我有不少,需要的话传给你
全部回答
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 *
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
如果突然间发现女朋友喜欢听得都是邓丽君的歌
相约宾馆(铜仁碧江区)地址在哪,我要去那里办
小楼收费站怎么去啊,有知道地址的么
青龙商务宾馆(铜仁德江县)地址在什么地方,想
环境风险二级评价的基本内容包括()。A.风险
刚开始接触DNF,练了个狂战,现在才20,问一
萌发的近义词是什么?
鸿兴宾馆(铜仁沿河土家族自治县)地址在什么地
怎么样把电脑中F盘卸载
女,40岁,因“半年体重增加20kg,伴月经紊乱
百度地图api怎么设置默认的位置
平安客栈(铜仁沿河土家族自治县)地址有知道的
浩浩宾馆(铜仁德江县)地址在什么地方,想过去
青岛高层住房公摊比例一般是多少?
成龙酒店(铜仁石阡县)地址在什么地方,想过去
推荐资讯
饮和豆腐坊配送中心北环店这个地址在什么地方
诚信种子在什么地方啊,我要过去处理事情
【梦的解释】说文解字中的“梦”的解释
各位大神,跪求推荐首浪漫轻松点的音乐,用作
美之道美容养生会所地址在哪,我要去那里办事
永诚配货地址在什么地方,我要处理点事
脚动手术脱内裤干嘛
汽车加油不提速车是福特蒙迪欧2,3加油3一5千
安能物流(育才路)(东二环路与育才路交叉口旁)
小小美发店在什么地方啊,我要过去处理事情
南通市康恩诊所怎么去啊,有知道地址的么
商贸鱼行在什么地方啊,我要过去处理事情
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?