我有三个类来保存输入表单中的数据,然后每个类都向数据库提交一个插入查询。
使用现有对象和单个存储过程必须有更好的方法,但我不能让现有对象在另一个类中工作。我为问题的简单性道歉,因为我认为这是一个非常直接的解决方案,可以整理我的代码。
使用下面的代码我想要实现的是在StoredProc类中重用EndUser,Bank和Company的现有实例,这样我就不必在每个类方法中使用SQL查询而只需要一个保存方法来自StoredProc类。
修改 为了澄清数据库的东西,即SQL字符串不是问题,我想问的是我可以在storedproc类中使用现有对象(其中三个)的实例,这样我就可以使用一个(已经写入)存储程序
道歉代码有点长,但我尽可能地减少代码,同时仍然有意义(以及运行):
表单后端
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
EndUser newUser = new EndUser(textBox1.Text, textBox2.Text);
Company newcmpny = new Company(textBox4.Text, textBox3.Text);
Bank newbank = new Bank(textBox6.Text, textBox5.Text);
newUser.Save();
newcmpny.Save();
newbank.Save();
}
}
DataHold类(全部在一个文件中)
class EndUser
{
public EndUser(string first, string last) {
firstName = first;
lastName = last;
}
public int iD { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public void Save()
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"INSERT INTO [EndUser] (FirstName, LastName)
VALUES (@FirstName, @LastName)");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("FirstName", firstName).ToString();
command.Parameters.AddWithValue("LastName", lastName).ToString();
command.ExecuteNonQuery();
}
}
}
}
class Company
{
public Company(string cmpny, string tele)
{
company = cmpny;
telephone = tele;
}
public string company { get; set; } // textbox4
public string telephone { get; set; } // textbox3
public void Save()
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"INSERT INTO [Company] (CName, Telephone)
VALUES (@CName, @Telephone)");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("CName", company).ToString();
command.Parameters.AddWithValue("Telephone", telephone).ToString();
command.ExecuteNonQuery();
}
}
}
}
class Bank
{
public Bank(string bn, string scode)
{
name = bn;
sortcode = scode;
}
public string name { get; set; } // textbox6
public string sortcode { get; set; } // textbox5
public void Save()
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"INSERT INTO [Bank] (BankName, SortCode)
VALUES (@BankName, @SortCode)");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("BankName", name).ToString();
command.Parameters.AddWithValue("SortCode", sortcode).ToString();
command.ExecuteNonQuery();
}
}
}
}
class StoredProc
{
public void ToTheDB()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
con.Open();
using(SqlCommand cmd = new SqlCommand("Procedure",con))
{
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("FirstName", newUser.firstName);
cmd.ExecuteNonQuery();
}
}
}
}
}
答案 0 :(得分:5)
首先 - IMO是一个编写得很糟糕的代码。
我的建议:
您已经问过:"我可以在storedproc类中使用现有对象(其中三个)的实例,这样我就可以使用一个(已经编写的)存储过程"
答案是 - 你很难以这种方式使用现有的代码。就我所见,你没有存储过程。你只有SQL查询的字符串。
无论如何,您可以尝试将您的类用作storedproc类中的模型,并创建使用它们的新存储过程。
看起来应该是这样的:
class StoredProc
{
public void ToTheDB(EndUser endUser, Company company, Bank bank)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
con.Open();
using(SqlCommand cmd = new SqlCommand("Procedure",con))
{
cmd.CommandType = CommandType.StoredProcedure;
//Here you can use data from your "model" classes and add them as parameters for your stored procedure.
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = endUser.firstName;
//the rest of the parameters from EndUser, Company and Bank classes
cmd.ExecuteNonQuery();
}
}
}
}
再一次,你应该将你的逻辑和模型分开。
这是一个示例模型:
public class Bank
{
public string name { get; set; }
public string sortCode { get; set; }
}
Ant这是数据访问层或存储库的方法:
void AddBank(Bank bank)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
con.Open();
//Procedure for inserting
using(SqlCommand cmd = new SqlCommand("Procedure",con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = bank.name;
cmd.ExecuteNonQuery();
}
}
}
答案 1 :(得分:2)
许多设计原则实际上都避免在现在的对象中进行CRUD操作,而更愿意接受该对象作为数据服务层的参数。您的代码实际上应该更大,而不是更小。示例服务将是:
public Bank(string bn, string scode)
{
name = bn;
sortcode = scode;
}
public string name { get; set; } // textbox6
public string sortcode { get; set; } // textbox5
}
class BankDataService
{
public void SaveNewBankToDatabase(Bank bank) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"INSERT INTO [Bank] (BankName, SortCode)
VALUES (@BankName, @SortCode)");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("BankName", bank.name);
command.Parameters.AddWithValue("SortCode", bank.sortcode);
command.ExecuteNonQuery();
}
}
}
public void UpdateBankToDatabase(Bank bank) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"UPDATE [Bank]
SET SortCode=@SortCode
WHERE @BankName=@BankName");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("BankName", bank.name);
command.Parameters.AddWithValue("SortCode", bank.sortcode);
command.ExecuteNonQuery();
}
}
}
public void SelectBankFromDatabase(string bankName) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabase"].ConnectionString))
{
string sqlQuery = (@"SELECT BankName, SortCode FROM [Bank] WHERE BankName=@BankName");
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
command.Parameters.AddWithValue("BankName", bank.name);
using(var reader = command.ExecuteReader()) {
if(reader.Read()){
return new Bank { BankName=reader["BankName"].ToString(), SortCode=reader["SortCode"].ToString(); }
}
}
}
return null;
}
}
}