如果我在一个单独的文件中使用连接类,ADO.NET如何添加参数?

时间:2012-05-23 11:16:25

标签: c# ado.net

如果我在一个单独的文件中使用类,ADO.NET如何添加参数?

类别:

class SQLconnect
{
    public static void Sql(string Command_Text)
    {
        string connectionPath =
            "Data Source=USER\\SQLEXPRESS;Initial Catalog=db;Integrated Security=SSPI;";

        SqlConnection Connection = new SqlConnection(connectionPath);

        Connection.Open();

        SqlCommand Command = Connection.CreateCommand();
        Command.CommandText = Command_Text;
        Command.ExecuteNonQuery();

        Connection.Close();
    }
}

参数:

        SQLconnect.Sql("INSERT INTO [dbo].[work] ([name],[code])VALUES(@name, @code)");

        SqlParameter param = new SqlParameter();
        param.ParameterName = "@name";
        param.Value = nameTextBox.Text;
        param.SqlDbType = SqlDbType.Text;
        //   Parameters.Add(param);


        param = new SqlParameter();
        param.ParameterName = "@code";
        param.Value = codeTextBox.Text;
        param.SqlDbType = SqlDbType.Text;
        //   Parameters.Add(param);

3 个答案:

答案 0 :(得分:3)

一种选择是更新您的SqlConnect.Sql()方法以接受一组参数:

class SQLconnect
{
    public static void Sql(string Command_Text, params SqlParameter[] parameters)
    {
        string connectionPath =
            "Data Source=USER\\SQLEXPRESS;Initial Catalog=db;Integrated Security=SSPI;";

        SqlConnection Connection = new SqlConnection(connectionPath);

        Connection.Open();

        SqlCommand Command = Connection.CreateCommand();
        Command.CommandText = Command_Text;

        if(parameters != null && parameters.Length > 0) 
        {
          foreach(var p in parameters)
            Command.Parameters.Add(p);
        }


        Command.ExecuteNonQuery();

        Connection.Close();
    }
}

然后您的调用代码将是这样的:

    SqlParameter param1 = new SqlParameter();
    param1.ParameterName = "@name";
    param1.Value = nameTextBox.Text;
    param1.SqlDbType = SqlDbType.Text;

    param2 = new SqlParameter();
    param2.ParameterName = "@code";
    param2.Value = codeTextBox.Text;
    param2.SqlDbType = SqlDbType.Text;

    SQLconnect.Sql("INSERT INTO [dbo].[work] ([name],[code])VALUES(@name, @code)", param1, param2);

答案 1 :(得分:1)

这是我的版本希望它有所帮助

public class SqlConnect
{
    public string ConnectionString { get; private set; }
    public string CommandText { get; private set; }
    public SqlParameterCollection Parameters { get; private set; }

    public SqlConnect(string connectionString, string commandText)
    {
        ConnectionString = connectionString;
        CommandText = commandText;
        Parameters = null;
    }

    public SqlConnect(string connectionString, string commandText, SqlParameterCollection parameters)
        : this(connectionString, commandText)
    {
        Parameters = parameters;
    }

    public int Execute()
    {
        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = CommandText;

            foreach (var sqlParameter in Parameters)
            {
                command.Parameters.Add(sqlParameter);
            }

            int rowsAffected = command.ExecuteNonQuery();
            connection.Close();
            return rowsAffected;
        }
    }
}

答案 2 :(得分:0)

您可以为数据库连接创建单独的类...然后为每个参数创建一个属性,如下所示

public class StudDataAccess
{
    public string connectionString = "Data Source=USER\\SQLEXPRESS;Initial Catalog=db;Integrated Security=SSPI;";
    public int pStudId
    {
        set;
        get;
    }
    public string pStudName
    {
        set;
        get;
    } 
    public void NewStudent()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand("AddStudent", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@StudId", SqlDbType.Int).Value = pStudId;
        cmd.Parameters.Add("@StudName", SqlDbType.VarChar, 50).Value = pStudName;            
        cmd.Prepare();
        cmd.ExecuteNonQuery();
        conn.Close();
    }  
}

这里我们创建了两个参数id,name,这样您就可以从需要插入的类中设置它的值,选择数据。如下创建该类的对象

        StudDataAccess objDataAccess = new StudDataAccess();
        objDataAccess.pStudId =Convert.ToInt32(TextBox1.Text);
        objDataAccess.pStudName = TextBox2.Text;           
        objDataAccess.NewStudent(); //Call addstudent store procedure with parameters