mardi 4 août 2015

Scalar variable when using class

Do I need to add all the parameter I intend to use throughout the code in the class that have the SqlCommand, or can I pass the parameters when I call for the class?

In my Class file (Called DataAccess):

public static int SqlGetInt(string queryString)
    {
        int retVal = 0;

        try
        {
            using (SqlConnection cn = GetConnection())
            {
                cn.Open();
                using (SqlTransaction tr = cn.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    using (SqlCommand cmd = new SqlCommand(queryString, cn))
                    {
                        cmd.Transaction = tr;

                        using (SqlDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.Read())
                                retVal = GetRdrInt(rdr, 0);
                        }
                    }
                    tr.Commit();
                }
            }
        }

in my Form1 code:

string sql = "SELECT ID FROM tbl_employees WHERE username = @username";
        _responsible_id = DataAccess.SqlGetInt(sql);

Since I havent added the parameters, this ofcourse fails. So back to my question: Can I add these parameters in my Form1 file when I call for SqlGetInt() or do I need to add it in the SqlGetInt() in the Class file?

Aucun commentaire:

Enregistrer un commentaire