谁会用SQL Server做一个简单的数据库系统?只需要实现简单的查询删除,添加修改等简单的功能就可以,很急啊

 我来答
coder_tmp
2012-05-24 · TA获得超过443个赞
知道小有建树答主
回答量:547
采纳率:100%
帮助的人:353万
展开全部
把下面这段代码放在一个Class里面,同时修改相应的连接串和表名称表字段,就可以实现增加删除修改查询四个功能:
------------------------------在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;
}
}
追问
怎么修改?还有在什么中调用啊,在web中实现是什么?
追答
1.数据库建表,
create table mytable(
pkid nvarchar(40) not null,
a nvarchar(40) null,
b nvarchar(40) null,
c nvarchar(40) null
)
2.新建对应此表的实体类
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _a; }
set { _a = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
private String _b = "";
public String b
{
get { return _b; }
set { _b = value; }
}
private String _c = "";
public String c
{
get { return _c; }
set { _c = 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,b,c) values(@pkid,@a,@b,@c)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@b", obj.b);
cmd.Parameters.AddWithValue("@c", obj.c);
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();
---------内容未完,只允许写999个字,没办法,还有一大半放不上去。
百度网友491f1c2
2012-05-22 · TA获得超过2297个赞
知道大有可为答主
回答量:2504
采纳率:62%
帮助的人:2260万
展开全部
不懂你的问题。sql的语句就可以实现你说的功能了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
nc88918
2012-05-22 · 超过28用户采纳过TA的回答
知道答主
回答量:129
采纳率:0%
帮助的人:71.3万
展开全部
是写SQL语句吗
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式