如何将SQL参数与NpgsqlDataAdapter一起使用?

如何将SQL参数与NpgsqlDataAdapter一起使用?,sql,parameters,npgsql,Sql,Parameters,Npgsql,是否可以像使用NpgsqlDataAdapter一样,将参数与NpgsqlDataAdapter一起使用: string sql = "SELECT * FROM tbl_student WHERE name = @val"; NpgsqlCommand command = new NpgsqlCommand(sql, conn); command.Parameters.AddWithValue("@val", name); 我有一

是否可以像使用NpgsqlDataAdapter一样,将参数与NpgsqlDataAdapter一起使用:

          string sql = "SELECT * FROM tbl_student WHERE name = @val";
          NpgsqlCommand command = new NpgsqlCommand(sql, conn); 
          command.Parameters.AddWithValue("@val", name);
我有一段代码,它显示了我在gridview中看到的学生的信息:

            string sql = "SELECT * FROM tbl_student WHERE studentname = '" + name + "'";               
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);    
            ds.Reset(); 
            da.Fill(ds);   // filling DataSet with result from NpgsqlDataAdapter
            dt = ds.Tables[0]; // select select first column
            GridView1.DataSource = dt;   //connect grid to DataTable           
            GridView1.DataBind();
我的问题是:我能否以某种方式使用参数(如上面的示例)而不是在SQL中使用“+name+”? 我已经学会了如何使用参数,在使用NpgsqlDataAdapter时是否也需要参数


谢谢。

我以前也有同样的问题,我就是这么想的:

    string sql = "SELECT * FROM tbl_student WHERE studentname = @param";
    NpgsqlCommand command = new NpgsqlCommand(sql,conn);
    command.Parameters.Add("@param", textBox_studentname.Text); //add the parameter into the sql command

    DataTable dt = new DataTable();
    //load the DataReader object of the command to the DataTable
    dt.Load(NpgsqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection));
    GridView1.DataSource = dt;

你可以阅读和阅读。

我以前也有同样的问题,这就是我想到的:

    string sql = "SELECT * FROM tbl_student WHERE studentname = @param";
    NpgsqlCommand command = new NpgsqlCommand(sql,conn);
    command.Parameters.Add("@param", textBox_studentname.Text); //add the parameter into the sql command

    DataTable dt = new DataTable();
    //load the DataReader object of the command to the DataTable
    dt.Load(NpgsqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection));
    GridView1.DataSource = dt;

你可以阅读和阅读。

我以前也有同样的问题,这就是我想到的:

    string sql = "SELECT * FROM tbl_student WHERE studentname = @param";
    NpgsqlCommand command = new NpgsqlCommand(sql,conn);
    command.Parameters.Add("@param", textBox_studentname.Text); //add the parameter into the sql command

    DataTable dt = new DataTable();
    //load the DataReader object of the command to the DataTable
    dt.Load(NpgsqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection));
    GridView1.DataSource = dt;

你可以阅读和阅读。

我以前也有同样的问题,这就是我想到的:

    string sql = "SELECT * FROM tbl_student WHERE studentname = @param";
    NpgsqlCommand command = new NpgsqlCommand(sql,conn);
    command.Parameters.Add("@param", textBox_studentname.Text); //add the parameter into the sql command

    DataTable dt = new DataTable();
    //load the DataReader object of the command to the DataTable
    dt.Load(NpgsqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection));
    GridView1.DataSource = dt;
你可以阅读和阅读