选择命令参数C#

时间:2012-07-30 03:59:56

标签: c# asp.net c#-4.0

我需要获取SelectCommand.Parameters

的值 你能帮助我吗?

string sql = "SELECT area, login, senha FROM tbl_usuarios WHERE login = '" + login + "' AND senha = '" + senha + "'";
adapter = new SqlDataAdapter(sql, conn);
dataTable = new DataTable();

adapter.SelectCommand.Parameters.Add("login", DbType.String);
adapter.SelectCommand.Parameters.Add("senha", DbType.String);

string a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16); // Error Coversion
Session.Add("ar", a);

4 个答案:

答案 0 :(得分:2)

我认为问题在于您尝试将Add方法(也就是SqlParameter)的返回类型分配给字符串。

请参阅SqlParameterCollection.Add Method (String, SqlDbType)

我想要的东西就像是

SqlParameter a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16);

答案 1 :(得分:0)

检查MSDN上的代码:SqlDataAdapter.SelectCommand Property

也许你需要的是:

获取数据表中的值:

string sql = "SELECT area, login, senha FROM tbl_usuarios WHERE login = '" + login + "' AND senha = '" + senha + "'";
adapter = new SqlDataAdapter(sql, conn);
dataTable = new DataTable();
adapter.Fill(dataTable);

string a  = dataTable.Rows["area"].toString(); //get area value..
Session.Add("ar", a);

此致

答案 2 :(得分:0)

你的问题不清楚,但是Add方法返回一种SqlParameter,所以这个:

string a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16);

错了..它需要是:

SqlParameter a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16);

答案 3 :(得分:0)

        bool encontrou = false;
        string sql = "SELECT area, login, senha FROM tbl_usuarios WHERE login = '" + login + "' AND senha = '" + senha + "'";
        command = new SqlCommand(sql, conn);
        try
        {
            conn.Open();
            dataReader = command.ExecuteReader();
            while(dataReader.Read())
            {
                string area = dataReader["area"].ToString();
                Session.Add("area", area);
                encontrou = true;
            }
        }