Con.Open()和Close()在函数中,我该怎么办? [英] Con.Open() And Close() In Function, How Can I Do ?

查看:249
本文介绍了Con.Open()和Close()在函数中,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

  protected   void  Button2_Click(objectsender,EventArgs e)
{
connection();
query = Emplogin; // 存储过程名称
SqlCommand com = 新的 SqlCommand(query,con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue( @ Usename,TextBox1.Text.ToString() ); // 用于用户名
com.Parameters.AddWithValue( @ Password,TextBox2.Text.ToString()); // 密码

int usercount =( Int32 )com.ExecuteScalar(); // 用于获取单个值

if (usercount == 1 // 比较表中的用户
{
Response.Redirect( Welcome.aspx); // for sucsseful login
}
else
{
con.Close();
Label1.Text = 无效的用户名或密码; // 无效登录
}
}





i想创建像单一时间的项目只使用con.open()和con.close()整个项目,因为每个查询都应该打开连接并关闭,所以连接打开关闭使用as函数,请给出执行函数的代码?

可能吗?



问候

AR

解决方案

不,你不能跳过con.open()方法,因为它是你的应用程序可以与之通信的方式数据库。



如果您应该使用它,请尝试以下代码。

  if (con.state == connectionstate.close)
con.open();







由于某种原因,.Net Framework保留了一个连接池。相信它! :)你不必编写这么多代码来连接数据库并释放连接。



你可以使用'using'语句并休息请放心,'IDBConnection.Release()'将为您关闭连接。



<前一个=C#> 使用(SqlConnection conn = new SqlConnection(...))
{
// 无论在这里发生什么,连接都是
// 在最后处理(关闭)。
}





In这段代码你不再需要关闭连接。


你是否在询问是否可以在应用程序启动时打开一个连接并在应用程序的生命周期中保持打开?非常糟糕的主意!连接需要SQL Server的连接许可证,而且这些许可证并不便宜。占用连接并且不做任何操作会从SQL Server用户那里得到一些非常讨厌的消息。



最佳做法是使用连接只打开一段时间因为你需要它。这允许客户使用更少的连接许可证(并节省资金!),并允许更好地扩展应用程序施加的负载。



尽可能晚开放,运行您的查询并尽早关闭。


只是添加,将所有这些代码放在您的表示层中是一个坏主意。似乎只有一个按钮可以执行登录,但是如果要测试登录代码怎么办?如果它在您的表示层中的事件处理程序中,您如何做到这一点?如果你用一个proc然后想要在三个地方打电话怎么办?如果那个proc得到一个额外的参数怎么办?突然间,你到处寻找所有叫做proc的地方,而不是只有一个。


my code

protected void Button2_Click (objectsender, EventArgs e)
 {
     connection();
     query = "Emplogin";   //stored procedure Name
     SqlCommand com = new  SqlCommand(query, con);
     com.CommandType= CommandType.StoredProcedure;
     com.Parameters.AddWithValue("@Usename", TextBox1.Text.ToString());   //for username 
     com.Parameters.AddWithValue("@Password", TextBox2.Text.ToString());  //for password
 
     int usercount = (Int32)com.ExecuteScalar();// for taking single value
 
     if (usercount == 1)  // comparing users from table 
     {
         Response.Redirect("Welcome.aspx");  //for sucsseful login
     }
     else
     {
         con.Close();
         Label1.Text = "Invalid User Name or Password";  //for invalid login
     }
}



i want to create project like single time only use con.open() and con.close() entire project, because of each and every query should have the connection open and close, so the connection open close using as function, please give the code of the execute function ?
that is possible ??

Regards
AR

解决方案

No you can not skip con.open() method , because it is the way that your application can communicate with the database.

And if you should use it try following code.

if(con.state==connectionstate.close)
       con.open();




The .Net Framework mantains a connection pool for a reason. Trust it! :) You don't have to write so much code just to connect to the database and release the connection.

You can just use the 'using' statement and rest assured that 'IDBConnection.Release()' will close the connection for you.

using (SqlConnection conn = new SqlConnection (...))
{
    // Whatever happens in here, the connection is 
    // disposed of (closed) at the end.
}



In this code you dont need to close connection anymore.


Are you asking if you can open a connection at the start of your application and leave it open for the life of the application?? VERY bad idea! The connection requires a connection license to the SQL Server and those don't come cheap. Hogging a connection and doing nothing with it will get you some very nasty messages from your SQL Server people.

Best practice is to use have the connection open only for as long as you need it. This allows a customer to use fewer connection licenses (and save money!) and allows for much better scaling of the load imposed by the application.

Open as late as possible, run your query and close as early as possible.


Just to add, putting all this code in your presentation layer is a bad idea. It seems likely that only one button will ever perform a login, but what if you want to TEST your login code ? How do you do that, if it's inside an event handler in your presentation layer ? What if you do this with a proc that you then want to call in three places ? What if that proc gets an extra parameter ? Suddenly you're searching everywhere for all the places that call the proc, instead of only having one.


这篇关于Con.Open()和Close()在函数中,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆