高分求ASP.NET(C#)数据库基本操作里面的插入或删除的代码,需要aspx和aspx.cs的完整代码。谢谢
6个回答
展开全部
1)直接执行sql语句
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('haha')");
db.ExecuteNonQuery(cmd);
2)执行存储过程
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t1_insert");
db.AddInParameter(cmd, "@id", DbType.String, "heihei");
db.ExecuteNonQuery(cmd);
3)返回DataSet
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("select * from t1");
DataSet ds = db.ExecuteDataSet(cmd);
4)返回DataTable
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("t1_findall");
DataTable dt = db.ExecuteDataTable(cmd);
5)输入参数/输出参数/返回值的使用(比较重要哦)
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
db.ExecuteNonQuery(cmd);
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
6)DataReader使用
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
using (DbDataReader reader = db.ExecuteReader(cmd))
{
dt.Load(reader);
}
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
7)事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)
pubic void DoBusiness()
{
using (Trans t = new Trans())
{
try
{
D1(t);
throw new Exception();//如果有异常,会回滚滴
D2(t);
t.Commit();
}
catch
{
t.RollBack();
}
}
}
public void D1(Trans t)
{
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd,t);
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
}
public void D2(Trans t)
{
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('..')");
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd, t);
}
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('haha')");
db.ExecuteNonQuery(cmd);
2)执行存储过程
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t1_insert");
db.AddInParameter(cmd, "@id", DbType.String, "heihei");
db.ExecuteNonQuery(cmd);
3)返回DataSet
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("select * from t1");
DataSet ds = db.ExecuteDataSet(cmd);
4)返回DataTable
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("t1_findall");
DataTable dt = db.ExecuteDataTable(cmd);
5)输入参数/输出参数/返回值的使用(比较重要哦)
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
db.ExecuteNonQuery(cmd);
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
6)DataReader使用
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
using (DbDataReader reader = db.ExecuteReader(cmd))
{
dt.Load(reader);
}
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
7)事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)
pubic void DoBusiness()
{
using (Trans t = new Trans())
{
try
{
D1(t);
throw new Exception();//如果有异常,会回滚滴
D2(t);
t.Commit();
}
catch
{
t.RollBack();
}
}
}
public void D1(Trans t)
{
DbHelper db = new DbHelper();
DbCommand cmd = db.GetStoredProcCommond("t2_insert");
db.AddInParameter(cmd, "@timeticks", DbType.Int64, DateTime.Now.Ticks);
db.AddOutParameter(cmd, "@outString", DbType.String, 20);
db.AddReturnParameter(cmd, "@returnValue", DbType.Int32);
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd,t);
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
}
public void D2(Trans t)
{
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('..')");
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd, t);
}
展开全部
试试这个行不?
1、执行查询操作:(ExecuteReader方法)
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/access.mdb"; //使用相对路径连接数据库
string mySelectQuery = "select * from login where userName ='lwjj'";//查询语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
//GetString(0,1,2....)
}
myReader.Close();
2、执行增、删、改操作:(ExecuteNonQuery方法)
private void dbcz()
{
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/IPTJ.mdb"; //Data Source = 数据库绝对路径
string myExecuteQuery = "insert into accIP (IP,地址) values ('" + 参数1+ "','" + 参数2+ "')";//增、删、改等语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();//返回影响的行数
myConnection.Close();
}
如果不行的话:试一下这个
在C#中使用控件DataGridView实现数据库增删改查
http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008219/100217.html
1、执行查询操作:(ExecuteReader方法)
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/access.mdb"; //使用相对路径连接数据库
string mySelectQuery = "select * from login where userName ='lwjj'";//查询语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
//GetString(0,1,2....)
}
myReader.Close();
2、执行增、删、改操作:(ExecuteNonQuery方法)
private void dbcz()
{
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/IPTJ.mdb"; //Data Source = 数据库绝对路径
string myExecuteQuery = "insert into accIP (IP,地址) values ('" + 参数1+ "','" + 参数2+ "')";//增、删、改等语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();//返回影响的行数
myConnection.Close();
}
如果不行的话:试一下这个
在C#中使用控件DataGridView实现数据库增删改查
http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008219/100217.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这些代码还真不少,一般都把数据库操作和页面操作分开的,不是放在一起。你可以到51aspx直接下载这方面的代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-11-08
展开全部
现在很少有人直接把数据库操作直接写到aspx页面上的。你到楼上说的那个站去找找,看有没有这种代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据库中的表结构是什么样子的?然后才能能帮你写向数据库中插入或删除的数据的代码啊?
更多追问追答
追问
例如我用SQL建了一个名字为news的表,列名为name,然后在aspx里显示这些内容,建个按钮可以选择性的删除某条内容,帮我写下aspx和.cs里的代码
追答
可以使用Repeater控件来实现(假设你在SQL Server 2000中创建的数据库名为student,里面有一个表news,列名为ID(主键),name)
aspx代码如下:
无标题页
姓名操作
' OnClientClick="Return confirm('确实要删除吗?')" Text="删除" />
"
SelectCommand="SELECT * FROM [news]">
.cs代码超过字数,只好截图了。
如果有什么问题,还可以问我...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询