110,534
社区成员
发帖
与我相关
我的任务
分享
class Conn
{
private static string cn = "Data Source=192.168.1.8,1433;Initial Catalog=Data2020;User ID=sa;pwd=abc123";
public static SqlConnection rs = new SqlConnection(cn);
}
//例子1
try
{
string sql = "Select * From tTable Where tID='1'";
SqlCommand cmd = new SqlCommand(sql, Conn.rs);
Conn.rs.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
this.Text = dr["LoginTitle"].ToString();
dr.Close();
cmd.Clone();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
finally
{
Conn.rs.Close();
}
//例子2
try
{
string sql = "Select * From tTable Where tID='1'";
using (SqlCommand cmd = new SqlCommand(sql, Conn.rs))
{
Conn.rs.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
this.Text = dr["LoginTitle"].ToString();
dr.Close();
}
cmd.Clone();
}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
finally
{
Conn.rs.Close();
}
//例子3
try
{
string sql = "Select * From tTable Where tID='1'";
using (SqlCommand cmd = new SqlCommand(sql, Conn.rs))
{
Conn.rs.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
this.Text = dr["LoginTitle"].ToString();
dr.Close();
}
cmd.Clone();
Conn.rs.Close();
}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
public static SqlConnection rs ..................
这种代码就是错误的模式。