尝试在SqlDataAdapter中传递SqlCommand作为参数

时间:2017-06-07 02:45:36

标签: c# asp.net sql-server sqldataadapter

我已经成功构建了执行select命令的方法。它工作正常。然后我更改了SqlDataAdapter DA = new SqlDataAdapter();

的代码

我尝试将SqlCommand作为CommandType.Text传递给参数,但我无法成功完成。我收到错误。如果我可以将它作为参数传递,有什么办法吗?请看我的代码。

运行代码(aspx页码)

if ((!string.IsNullOrEmpty(user_login.Value)) && (!string.IsNullOrEmpty(user_pass.Value)))
{
        // username & password logic
        DataTable dt = new DataTable();
        string strQuery = "SELECT 1 FROM TBL_USER_INFO WHERE USERNAME = @USERNAME AND PASSWORD = @PASSWORD";

        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = user_login.Value.Trim();
        cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = user_pass.Value.Trim();

        DBConnection conn_ = new DBConnection();

        dt = conn_.SelectData(cmd);

        if (conn_.SQL_dt.Rows.Count > 0)
        {
            Response.Redirect("Home.aspx", false);
        }
    }

成功的连接类代码

public DataTable SelectData(SqlCommand command)
{
    try
    {
       conn.Open();

       SqlDataAdapter DA = new SqlDataAdapter();

       command.CommandType = CommandType.Text;
       command.Connection = conn;

       DA.SelectCommand = command;
       DA.Fill(SQL_dt);

       return SQL_dt;
   }
   catch (Exception ex)
   {
       return null;
   }
   finally
   {
       conn.Close();
   }
}

如何将CommandType.Text作为SqlDataAdapter的参数传递?

错误代码

public DataTable SelectData(SqlCommand command)
{
    try
    {
        conn.Open();

        SqlDataAdapter DA = new SqlDataAdapter(command.CommandType.ToString(), conn);

        // command.CommandType = CommandType.Text;
        // command.Connection = conn;
        DA.SelectCommand = command;
        DA.Fill(SQL_dt);

        return SQL_dt;
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        conn.Close();
    }
}

我收到此错误:

  

System.InvalidOperationException:Fill:SelectCommand.Connection属性尚未初始化。
  在System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter适配器,IDbCommand命令,String方法)......

4 个答案:

答案 0 :(得分:1)

return/rewrite original response before proxy

使用:

public DataTable SelectData(string query)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection("Your Connection String here"))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                        return dt;
                    }
                }
            }
        }

答案 1 :(得分:1)

红人有答案。只是为了清理代码......

    public DataTable SelectData(string query)
    {
        using (var connection = new SqlConnection("myConnectionString"))
        using (var command = new SqlCommand(query, connection))
        using (var adapter = new SqlDataAdapter(command))
        {
            var dt = new DataTable();

            connection.Open();
            adapter.Fill(dt);

            return dt;
        }
    }

答案 2 :(得分:0)

实际上你应该在SQLCommand.Hope上传递连接对象它帮助你

 DBConnection conn_ = new DBConnection();
SqlCommand cmd = new SqlCommand(strQuery,conn_);

答案 3 :(得分:-1)

您获得的错误与CommandType.Text无关,它表示您已初始化SelectCommand的连接属性。基本上你应该取消注释" command.Connection = conn;"摆脱这个错误。如果您仍然面临任何其他问题,最好在问题中提供这些详细信息以提供准确答案。