多SQL查询asp.net c#
我需要在一个函数中运行多个查询,是否必须为每个查询创建一个新的SqlConnection?或者有一个连接但不同的SqlCommands也可以工作 谢谢 编辑:这行吗多SQL查询asp.net c#,c#,asp.net,sql,C#,Asp.net,Sql,我需要在一个函数中运行多个查询,是否必须为每个查询创建一个新的SqlConnection?或者有一个连接但不同的SqlCommands也可以工作 谢谢 编辑:这行吗 using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query1, conn))
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query1, conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(query2, conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(query3, conn))
{
cmd.ExecuteNonQuery();
}
}
使用一个
SqlConnection
和多个SqlCommands
可以很好地工作,但是,在尝试运行其他命令之前,必须确保处理从以前的命令返回的任何SqlDataReaders
using (SqlConnection conn = new SqlConnection())
{
conn.Open()
using (SqlCommand cmd = new SqlCommand("SELECT myrow FROM mytable", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Handle first resultset here
}
}
using (SqlCommand cmd = new SqlCommand("SELECT otherrow FROM othertable", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Handle second resultset here
}
}
}
或者,您可以将命令组合成一个批处理,而不是处理多个结果集,如下所示:
using (SqlConnection conn = new SqlConnection())
{
conn.Open()
using (SqlCommand cmd = new SqlCommand("SELECT myrow FROM mytable; SELECT otherrow FROM othertable", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Handle first resultset here, and then when done call
if (reader.NextResult())
{
// Handle second resultset here
}
}
}
}
当您处理许多结果集时,您会发现将这样的查询批处理在一起可以显著提高性能,但这样做的代价是增加了调用代码的复杂性。使用作为基础:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql1 = "SELECT ID,FirstName,LastName FROM VP_PERSON";
string sql2 = "SELECT Address,City,State,Code FROM VP_ADDRESS";
using (SqlCommand command = new SqlCommand(sql1,connection))
{
//Command 1
using (SqlDataReader reader = command.ExecuteReader())
{
// reader.Read iteration etc
}
} // command is disposed.
using (SqlCommand command = new SqlCommand(sql2,connection))
{
//Command 1
using (SqlDataReader reader = command.ExecuteReader())
{
// reader.Read iteration etc
}
} // command is disposed.
// If you don't using using on your SqlCommands you need to dispose of them
// by calling command.Dispose(); on the command after you're done.
} // the SqlConnection will be disposed
只打开一个SQLConnection 使用keyworkd使用,因为它会自动处理连接 如果您为每个连接打开连接,它可能会有性能问题 例如:
using (SqlConnection con = new SqlConnection(connectionString))
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code shows how you can use an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("{0} {1} {2}",
reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
}
}
}
还有一个例子:
public DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("your connection here")
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "your stored procedure here";
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
仅作为使用语句的的替代方案:
SqlConnection con = new SqlConnection(myConnectionString);
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = @"SELECT [stuff] FROM [tableOfStuff]";
con.Open();
SqlDataReader dr = null;
try
{
dr = cmd.ExecuteReader();
while(dr.Read())
{
// Populate your business objects/data tables/whatever
}
}
catch(SomeTypeOfException ex){ /* handle exception */ }
// Manually call Dispose()...
if(con != null) con.Dispose();
if(cmd != null) cmd.Dispose();
if(dr != null) dr.Dispose();
这与using语句之间的主要区别在于,这将允许您更干净地处理异常。无论您选择哪种方式
SqlConnections
由操作系统合并。您可以连续数千次打开和关闭连接,而不会招致任何性能或其他惩罚
其工作原理是:
var c=new-SqlConnection(…)
)c.Dispose()
)请记住,您不希望在代码中保留SqlConnection对象的时间超过绝对必要的时间。这可能会导致许多潜在问题,尤其是在进行web开发时。这意味着您的代码快速连续地打开和关闭100个SqlConnection对象要比抓住该对象并通过各种方法传递要好得多。最好向我们展示您的代码,然后我们可以给您提供更好的建议。我看到了您的另一个问题,不要将值作为内联传递给sql statemetns,它可能导致sql注入。如果您使用将所有内容都放在其中,则不需要处理它。SqlConnection和SqlCommand实现IDisposable。删除“conn.Close();conn.Dispose();cmd.Dispose();”:我是asp.net的新手,还没有介绍过SqlDataReaders。目前我只使用SqlConnection,然后通过组合查询和新连接创建SqlCommand,然后使用SqlDataAdapter检索数据或ExecuteOnQuery。我应该只确保处理连接和命令吗?@QPTR我以前没有使用过
SqlDataAdapter
,因此我不知道,但我相信您必须先处理SqlDataAdapter
,然后才能运行另一个查询。很抱歉,我不太清楚这一点-不过,这方面的其他人应该可以帮助您。@qptr,使用自动处理,不需要做任何额外的事情。很好,我将尝试将我的查询执行代码放入using中,非常感谢您的帮助!我可以这样使用它吗:“SqlConnection con=newsqlconnection(connectionString);con.Open();string cmdText=“query”SqlCommand cmd=new SqlCommand(cmdText,con);cmd.ExecuteNonQuery();cmd.Dispose();con.Close();con.Dispose();'然后创建另一个连接并重复该过程。我是asp.net新手,不了解DataReaders.QPTR的用法,不要创建另一个connectoin,它可能会有性能影响。为什么要打开另一个连接,你已经打开了一个到数据库的连接,是你想打开多个数据库。好的,那么我可以用你的方式,但在使用(SqlCommand=new…)部分只是添加执行和处置命令代码,而不是使用读卡器,我真的不明白它们的用法。谢谢你的帮助!@QPTR,是的,这很酷,如果是存储过程或sql插入,你可以使用nonexecute,如果它得到任何数据,如选择,你需要说执行查询,SLQDataAdapter用于执行和获取结果是一个datatable。我又为YouTube添加了一个示例。感谢您的帮助,我只是想知道我可以在使用(SqlCommand=new…)时在部分,仅查询执行和处置命令?放入您的SqlDataReader,使用它,然后处置它。或者更好地将SqlDataReader放入using中。感谢您的回复,我现在不使用SqlDataReader,但我会在完成此任务后立即阅读。感谢您的示例!注意,这样做意味着如果系统中出现任何错误