Home > Net >  Consult in c # using the correct way of using the database operation
Consult in c # using the correct way of using the database operation

Time:11-17

Consult in c # using the correct way of using the database operation
Create a class in c # Conn set the cn to private good, is this cn to public
 
The class Conn
{
Private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD=abc123 ";
Public static SqlConnection rs=new SqlConnection (cn);
}


Create a form Form1 three example code, written into the Form1_Load use using (.. ) {.. } to manipulate the database,
 
//example 1
Try
{
String SQL="Select * From tTable Where tID='1'";
SqlCommand CMD=new SqlCommand (SQL, Conn. Rs);
Conn. Rs. The Open ();
SqlDataReader Dr=CMD. ExecuteReader ();
Dr. Read ();
. This Text=Dr [r]. "LoginTitle" ToString ();
Dr. Close ();
CMD. The Clone ();
}
The catch (Exception er)
{
MessageBox. Show (er. Message);
}
The finally
{
Conn. Rs. The Close ();
}


//example 2
Try
{
String SQL="Select * From tTable Where tID='1'";
Using (SqlCommand CMD=new SqlCommand (SQL, Conn. Rs))
{
Conn. Rs. The Open ();
Using (SqlDataReader Dr CMD=ExecuteReader ())
{
Dr. Read ();
. This Text=Dr [r]. "LoginTitle" ToString ();
Dr. Close ();
}
CMD. The Clone ();
}
}
The catch (Exception er)
{
MessageBox. Show (er. Message);
}
The finally
{
Conn. Rs. The Close ();
}


//example 3
Try
{
String SQL="Select * From tTable Where tID='1'";
Using (SqlCommand CMD=new SqlCommand (SQL, Conn. Rs))
{
Conn. Rs. The Open ();
Using (SqlDataReader Dr CMD=ExecuteReader ())
{
Dr. Read ();
. This Text=Dr [r]. "LoginTitle" ToString ();
Dr. Close ();
}
CMD. The Clone ();
Conn. Rs. The Close ();
}
}
The catch (Exception er)
{
MessageBox. Show (er. Message);
}


Consult the using examples which is the correct way of using? Just calculate correct operation database,
Or, you have a better write the using method correctly? Help me write correct,
Thank you all!

CodePudding user response:

Use using at the end of the range release object automatically, need not to write the close

CodePudding user response:

Write the
 public static SqlConnection rs... 
the code is the wrong model,

SqlConnection itself is based on the caching mechanism, it automatically according to the connection string to the underlying physical connection to the cache, when you are new to a number of high-level "logical connection", the system will be according to the actual situation to reuse the underlying physical connection, such as your program to quickly create over 1000 SqlConnecttion object, but the runtime actually used six the underlying physical connection is enough, and not because "concurrent, nested" and so on to access the database connection in the structure is occupied "database connection" such a system crash is unusual, only every time new a SqlConnection is really know true to reuse the database connection, can really use the system mechanism, and declare a static database connection object, but the gild the lily, lead to abnormal crash, nor can reuse database physical connection,

CodePudding user response:

Any code you write is very tedious, illogical, looks not through the test of a certain commercial development experience, a basic logic is that if the connection is to define a static object, but in the process of a split open to Conn. Rs. The Close () this kind of action, this let a person feel is wulitou, better don't know what to say, as if there is no logical people cannot study process,

CodePudding user response:

Using is a syntactic sugar, make you less write two lines of code, using instance will be in the place where the curly brace call close automatically,

CodePudding user response:

Using is not used for database operation, is used to manage the life cycle of a database object, see:
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/using-statement

CodePudding user response:

Using (... ) the inside of the object is not need to explicitly call the Close or Dispose method (using automatically calls),

CodePudding user response:

Using is to manage the life cycle of an object, an object from start to finish in the using, do not need to manually to shut down, the GC will automatically recovery using the object
  •  Tags:  
  • C#
  • Related