Can I call a sql command parameter from another class?

眉间皱痕 提交于 2019-12-25 02:11:47

问题


I have 14 tables, with the usual sql common command parameters, insert, update etc. Beginners, like me, will have all the methods in the main class, like this...

namespace TestApp
{
 public partial class TestNamTxt : Form
 {
    private OleDbConnection myCon;

    public TestNamTxt()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      myCon = new OleDbConnection();
      myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C\:... 
                                 Database2.mdb")

      myCon.Open();
      ds1 = new DataSet();
      string sql = "SELECT * FROM Table1";
      da = new System.Data.OleDb.OleDbDataAdapter(sql,myCon);
      da.Fill(ds1, "Foo");
      myCon.Close();
   };

   private void Insertbtn_Click(object sender, EventArgs e)
   {
     OleDbCommand cmd = new OleDbCommand();
     cmd.CommandType = CommandType.Text;
     cmd.CommandText = "INSERT INTO Table1 (ID, Name)";
     cmd.Parameters.AddWithValue("@ID", IDTxt.Text);
     cmd.Parameters.AddWithValue("@Name", NameTxt.Text);
     cmd.Connection=myCon;   
     myCon.Open();
     cmd.ExecuteNonQuery();
     myCon.Close();
  }
}

Could I place code the above code in another class and in the Insertbtn method use the this method? Is there any tutorials or perhaps someone could demonstrate how this could be done? I am not sure what it is called on the description I have given here? Thanks in advance


回答1:


Sure you can. You can place GetConnection and Insert into separate class(or even leave in Form, but I don't recommend this) and use them as follows:

    public static OleDbConnection GetConnection()
    {
        var myCon = new OleDbConnection();
        myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C\:... Database2.mdb";

        return myCon;
    }
    public static void Insert(string id, string name)
    {
        var con = GetConnection();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Table1 (ID, Name)";
        cmd.Parameters.AddWithValue("@ID", id);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

    private void Insertbtn_Click(object sender, EventArgs e)
    {
        Insert(IDTxt.Text, NameTxt.Text);
    }

You can also specify Table name as method parameter if you need.




回答2:


If i have understood your question correctly
then ya you can do this. Basically you are trying to use DAL(Data Access Layer) the term used for this,

well its simple, place the above code into another class and then make an object of that class in this class and use it.

public class DataClass
{
  public static bool AddEmp(string id, string name)
   {
     bool result;
     OleDbCommand cmd = new OleDbCommand();
     cmd.CommandType = CommandType.Text;
     cmd.CommandText = "INSERT INTO Table1 (ID, Name)";
     cmd.Parameters.AddWithValue("@ID", id);
     cmd.Parameters.AddWithValue("@name", name);
     cmd.Connection=myCon;   

     try
      {
        myCon.Open();
        cmd.ExecuteNonQuery();
        result = true;
      }
     catch
      {
         result = false;
      }

  myCon.Close();
  return result;  
}

and then in the insert function do it like this

private void Insertbtn_Click(object sender, EventArgs e)
 {
    DataClass ob = new DataClass();
    bool returnResult = ob.AddEmp(IDtxt.txt, NameTxt.text)
    if(bool) // if result == true
     //dosomething
    else
    // do something
}

Hope it helps.




回答3:


Your TestNam class is derived from the Form class. Any form event handler you want to define must be a member function of TestNam, but within this function you can do what you want, including passing a reference to the active instance of the form.

If your functions are specific to the form class, put them in the class, if they're shared, you can put htem in another object.



来源:https://stackoverflow.com/questions/15705969/can-i-call-a-sql-command-parameter-from-another-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!