asp.net读取sql数据库时,能否同时执行两个搜索语句?怎么将两个搜索语句合在一起?跪求
stringstrconn=ConfigurationManager.ConnectionString["a"].ConnectionString;SqlConnecti...
string strconn = ConfigurationManager.ConnectionString["a"].ConnectionString;
SqlConnection conn= new SqlConnection(strconn);
conn.Open();
cmd.CommandText= "select a as 'a1' where id =1";//**
cmd.ExecuteNonQuery();
cmd.CommandText = "select a as 'a2' where id =2";//**
cmd.ExecuteNonQuery();
myreader= cmd.ExecuteReader();......
后面使用时,总是只能使用a1或a2中的一个,怎么将两个搜索语句放在一起?求指教 展开
SqlConnection conn= new SqlConnection(strconn);
conn.Open();
cmd.CommandText= "select a as 'a1' where id =1";//**
cmd.ExecuteNonQuery();
cmd.CommandText = "select a as 'a2' where id =2";//**
cmd.ExecuteNonQuery();
myreader= cmd.ExecuteReader();......
后面使用时,总是只能使用a1或a2中的一个,怎么将两个搜索语句放在一起?求指教 展开
展开全部
你好,可以把这两条Sql语句当成一个事务来执行,返回2个结果集,编写一个存储过程就可以实现。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection conn = new SqlConnection();
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["databaseConnectionString"].ConnectionString;
SqlCommand comm = new SqlCommand();
comm.CommandText = "select * from chengji;select * from xueji;";
comm.Connection = conn;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
do
{
if (reader.HasRows)
{
while (reader.Read())
{
string id1 = reader["课程"].ToString();
Response.Write(id1 + "<br/>");
}
}
} while (reader.NextResult() == false);
Response.Write("<br/>");
if (reader.HasRows)
{
while (reader.Read())
{
string id2 = reader["学号"].ToString();
Response.Write(id2 + "<br/>");
}
}
reader.Dispose();
reader.Close();
conn.Close();
参照上面这个例子,处理时
do
{
//a1的相关操作
}while (reader.NextResult() == false);
//a2的相关操作
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["databaseConnectionString"].ConnectionString;
SqlCommand comm = new SqlCommand();
comm.CommandText = "select * from chengji;select * from xueji;";
comm.Connection = conn;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
do
{
if (reader.HasRows)
{
while (reader.Read())
{
string id1 = reader["课程"].ToString();
Response.Write(id1 + "<br/>");
}
}
} while (reader.NextResult() == false);
Response.Write("<br/>");
if (reader.HasRows)
{
while (reader.Read())
{
string id2 = reader["学号"].ToString();
Response.Write(id2 + "<br/>");
}
}
reader.Dispose();
reader.Close();
conn.Close();
参照上面这个例子,处理时
do
{
//a1的相关操作
}while (reader.NextResult() == false);
//a2的相关操作
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Ado.Net 事务处理 失败之后回滚
var con = new SqlConnection {ConnectionString = DbConStr};
con.Open();
SqlTransaction tan = con.BeginTransaction();
SqlCommand cmd=new SqlCommand();
cmd.Connection = con;
cmd.Transaction = tan;
try
{
cmd.CommandText = "xxxxxxxxxxxxxxxx";
cmd.ExecuteNonQuery();
cmd.CommandText = "yyyyyyyyyyyyyyyyyyyy";
cmd.ExecuteNonQuery();
tan.Commit();
}
catch (Exception)
{
tan.Rollback();
throw;
}
var con = new SqlConnection {ConnectionString = DbConStr};
con.Open();
SqlTransaction tan = con.BeginTransaction();
SqlCommand cmd=new SqlCommand();
cmd.Connection = con;
cmd.Transaction = tan;
try
{
cmd.CommandText = "xxxxxxxxxxxxxxxx";
cmd.ExecuteNonQuery();
cmd.CommandText = "yyyyyyyyyyyyyyyyyyyy";
cmd.ExecuteNonQuery();
tan.Commit();
}
catch (Exception)
{
tan.Rollback();
throw;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select a as 'a1' where id in (1,2);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询