using (SqlConnection con = new SqlConnection("xxx")) { SqlCommand xx xxxxxxx........... }
先搞个数据库的命名空间不就可以USING啦.
using (SqlConnection con = new SqlConnection("xxx")) { .... }1.{....}需要SqlConnetcion这个资源 2.{....}这段语句执行完毕以后立即dispose SqlConnetcion 偶的理解,高手指证.
跟 try {} catch {} finally {} 功能一样
using (SqlConnection con = new SqlConnection("xxx")) { .... }1.{....}需要SqlConnetcion这个资源 2.{....}这段语句执行完毕以后立即dispose SqlConnetcion 偶的理解,高手指证. --------------------------------- 正解
{
SqlCommand xx xxxxxxx...........
}
{
....
}1.{....}需要SqlConnetcion这个资源
2.{....}这段语句执行完毕以后立即dispose SqlConnetcion
偶的理解,高手指证.
try
{}
catch
{}
finally
{}
功能一样
{
....
}1.{....}需要SqlConnetcion这个资源
2.{....}这段语句执行完毕以后立即dispose SqlConnetcion
偶的理解,高手指证.
---------------------------------
正解
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.Parameters.Add("@userName",SqlDbType.VarChar,50);
cmd.Parameters["@userName"].Value = userName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetUserInfo";
con.Open();
reader=cmd.ExecuteReader();
}
不用con.close(); ?
reader.close()用不用?
public void Page_Load(...)
{
SqlConnection sqlcon=new SqlConnection("数据库连接字符串");
sqlcon.open();//打开连接;}
public void Page_Load(...)
{
SqlConnection sqlcon=new SqlConnection("数据库连接字符串");
//由于重载构造函数,因此有多种初始化该类的方式
sqlcon.open();//打开连接;
SqlCommand cmd=new SqlCommand();//由于重载构造函数,因此有多种初始化该类的方式
cmd.CommandType=store;//表明是存在过程
cmd.CommandText="存储过程名称";
SqlDataReader sd=cmd.ExecuteReader();//执行
while(sd.read())//循环读取数据
{
response.write(sd["name"].tostring)
}
sd.close();
sqlconn.close();//关闭连接
}
因为他是Disponse并没有关闭
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = MyStoredProcedureName;
//int rowsAffected = cmd.ExecuteNonQuery();
// ...}
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SqlCommand object</param>
/// <param name="conn">SqlConnection object</param>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters to use in the command</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.CommandText = cmdText; if (trans != null)
cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null) {
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
} petshop 4.0中的,我只看见了open,没有看见close.所以偶认为dispose的前提需要关闭连接
先搞个数据库的命名空间不就可以USING啦.
======================================================汗!
using (SqlConnection con = new SqlConnection("xxx"))
{
....
}1.{....}需要SqlConnetcion这个资源
2.{....}这段语句执行完毕以后立即dispose SqlConnetcion
偶的理解,高手指证.
---------------------------------
正解
{
}