2017-06-14 55 views
0

我会做很多这样的事情,有人可以帮我把它转换成类吗?创建一个类来检查sqlserver中的值是否为空

private void CheckingForCostID() 
{ 
    //if CostID is exist then error. 
    newConnection.ConnectionM(); 
    SqlCommand cmd = SqlConnectionOLTP.cn.CreateCommand(); 
    cmd.CommandText = "SELECT CostCategoryID FROM CostCategory WHERE CostCategoryName [email protected] AND Description [email protected]"; 
    cmd.Parameters.AddWithValue("@costcategoryname", format.SetText(textBoxCostName)); 
    cmd.Parameters.AddWithValue("@description", format.SetText(textBoxCostDescription)); 
    var costid = cmd.ExecuteScalar(); 
    if (costid == null) 
    { 
     CreateCostCategoryData(); 
    } 
    else 
     MessageBox.Show("Cost Category already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    SqlConnectionOLTP.cn.Close(); 
} 

回答

-1

首先创建2类实体模型(CostCategoryModel) 第二类CostCategoryRepository。 在你的第一个定义结构的你对象在第二CostCategory 您定义的CRUD方法和您的事业的方法,如

public bool Exist(string name, string description){...} 

,那么你可以使用此示例代码模式

CostCategoryRepository repo = new CostCategoryRepository(); 
if(repo.Exist(name,description)) 
{ 
... 
}else{ 
... 
} 
+0

Newing了库?有人没有听说过单元测试,依赖注入,事务,工作单元,回滚。这是不好的建议。它应该是一个注入的'IRepository',至少。 – user9993

+0

他必须更改这段代码,他不重构所有项目结构我的解决方案不是最好的方法,但解决他的问题。 –