在我的 SqlCommand 的单独方法中连接到数据库
Connect to database in sepaerate method of my SqlCommand
我有一个表单可以在添加值之前检查它们是否在数据库中。每个字段都在不同的 table 中,为了保持一切整洁,我为每个字段设置了一个 checkExists
方法。有没有办法有一个连接到数据库的单独方法,这样我就不必在每个字段方法中都进行连接?
我想做这样的事情,这样我的代码就不那么乱了:
public void SetConnection()
{
SqlConnection myConnection =
new SqlConnection("user id=[username];" +
"password=[password];" +
"server=[server];" +
"database=[db_name];");
try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine("Unable to Connect");
}
}
public Boolean CheckData_Company(string[] items)
{
Class_DB set_conn = new Class_DB();
try
{
set_conn.SetConnection();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
//check that item does not already exist
string query_string = "SELECT * FROM CR_Company WHERE ([CompanyName] = @companyName";
SqlCommand check_Company = new SqlCommand(query_string, set_conn);
check_Company.Parameters.AddWithValue("@CompanyName", items[0]);
int CompanyExist = (int)check_Company.ExecuteScalar();
if(CompanyExist > 0)
{
return true;
}
else
{
return false;
}
}
但是我得到了
local variable set_conn
Argument 2: Cannot Convert from Class_DB to System.Data.SqlClient.SqlConnection
我明白这个错误,那么我该如何 return 正确的值,或者我是否必须在我的 CheckData_Comany()
方法中建立连接?
您的方法 SetConnection
应该像 SqlConnection
一样返回:
public SqlConnection SetConnection()
{
SqlConnection myConnection = new SqlConnection("user id=[username];" +
"password=[password];" +
"server=[server];" +
"database=[db_name];");
try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine("Unable to Connect");
}
return myConnection;
}
然后你可以得到类似的东西:
SqlConnection connection = set_conn.SetConnection();
然后将其作为参数传递给SqlCommand
构造函数:
SqlCommand check_Company = new SqlCommand(query_string, connection);
您的完整方法实现将变为:
public Boolean CheckData_Company(string[] items)
{
bool Exists = false;
Class_DB set_conn = new Class_DB();
SqlConnection connection = null;
try
{
connection = set_conn.SetConnection();
//check that item does not already exist
string query_string = "SELECT * FROM CR_Company WHERE ([CompanyName] = @companyName";
SqlCommand check_Company = new SqlCommand(query_string, set_conn);
check_Company.Parameters.AddWithValue("@CompanyName", items[0]);
int CompanyExist = (int)check_Company.ExecuteScalar();
if(CompanyExist > 0)
Exists = true;
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
connection.Close();
}
return Exists;
}
需要注意的重要一点是不要忘记通过调用 connection.Close()
最后关闭连接,否则它可能会导致在我们完成查询数据库时消耗不应该发生的资源,并且我们应该释放占用的资源。
我有一个表单可以在添加值之前检查它们是否在数据库中。每个字段都在不同的 table 中,为了保持一切整洁,我为每个字段设置了一个 checkExists
方法。有没有办法有一个连接到数据库的单独方法,这样我就不必在每个字段方法中都进行连接?
我想做这样的事情,这样我的代码就不那么乱了:
public void SetConnection()
{
SqlConnection myConnection =
new SqlConnection("user id=[username];" +
"password=[password];" +
"server=[server];" +
"database=[db_name];");
try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine("Unable to Connect");
}
}
public Boolean CheckData_Company(string[] items)
{
Class_DB set_conn = new Class_DB();
try
{
set_conn.SetConnection();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
//check that item does not already exist
string query_string = "SELECT * FROM CR_Company WHERE ([CompanyName] = @companyName";
SqlCommand check_Company = new SqlCommand(query_string, set_conn);
check_Company.Parameters.AddWithValue("@CompanyName", items[0]);
int CompanyExist = (int)check_Company.ExecuteScalar();
if(CompanyExist > 0)
{
return true;
}
else
{
return false;
}
}
但是我得到了
local variable set_conn
Argument 2: Cannot Convert from Class_DB to System.Data.SqlClient.SqlConnection
我明白这个错误,那么我该如何 return 正确的值,或者我是否必须在我的 CheckData_Comany()
方法中建立连接?
您的方法 SetConnection
应该像 SqlConnection
一样返回:
public SqlConnection SetConnection()
{
SqlConnection myConnection = new SqlConnection("user id=[username];" +
"password=[password];" +
"server=[server];" +
"database=[db_name];");
try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine("Unable to Connect");
}
return myConnection;
}
然后你可以得到类似的东西:
SqlConnection connection = set_conn.SetConnection();
然后将其作为参数传递给SqlCommand
构造函数:
SqlCommand check_Company = new SqlCommand(query_string, connection);
您的完整方法实现将变为:
public Boolean CheckData_Company(string[] items)
{
bool Exists = false;
Class_DB set_conn = new Class_DB();
SqlConnection connection = null;
try
{
connection = set_conn.SetConnection();
//check that item does not already exist
string query_string = "SELECT * FROM CR_Company WHERE ([CompanyName] = @companyName";
SqlCommand check_Company = new SqlCommand(query_string, set_conn);
check_Company.Parameters.AddWithValue("@CompanyName", items[0]);
int CompanyExist = (int)check_Company.ExecuteScalar();
if(CompanyExist > 0)
Exists = true;
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
connection.Close();
}
return Exists;
}
需要注意的重要一点是不要忘记通过调用 connection.Close()
最后关闭连接,否则它可能会导致在我们完成查询数据库时消耗不应该发生的资源,并且我们应该释放占用的资源。