跨类c多个对象使用#

Pur*_*rph 1 c#

我有三个类来保存输入表单中的数据,然后每个类都向数据库提交一个插入查询.

使用现有对象和单个存储过程必须有更好的方法,但我不能让现有对象在另一个类中工作.我为问题的简单性道歉,因为我认为这是一个非常直接的解决方案,可以整理我的代码.

使用下面的代码我想要实现的是在StoredProc类中重用EndUser,Bank和Company的现有实例,这样我就不必在每个类方法中使用SQL查询,只需要在StoredProc类中使用一个save方法.

编辑 为了澄清数据库的东西,即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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Lan*_*eyo 5

首先 - IMO是一个写得很糟糕的代码.

我的建议是:

  • 不要混合模型和SQL查询或任何数据库逻辑.
  • 不要在C#代码中使用纯SQL,而是使用实体框架或存储过程.
  • 不保存多个共享相同业务逻辑的实体,而不用单个事务包装它们.

您已经问过:"我可以在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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

再一次,您应该将逻辑和模型分开.

这是一个示例模型:

public class Bank
{
    public string name { get; set; }
    public string sortCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

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();
            }
        }
}
Run Code Online (Sandbox Code Playgroud)