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

4 回答 4

2

I think the problem is that you are trying to assign the return type from the Add Method (which is a SqlParameter) to a string.

See SqlParameterCollection.Add Method (String, SqlDbType)

I thing what you are looking for is something like

SqlParameter a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16);
于 2012-07-30T04:04:35.570 回答
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;
            }
        }
于 2012-07-30T12:15:20.050 回答
0

您的问题不清楚,但是 Add 方法返回一种 SqlParameter 类型,因此:

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

错了..它需要:

SqlParameter a = adapter.SelectCommand.Parameters.Add(@"area", DbType.Int16);
于 2012-07-30T04:06:39.967 回答
0

检查 MSDN 上的代码:SqlDataAdapter.SelectCommand 属性

也许你需要的是:

获取数据表上的值:

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);

问候

于 2012-07-30T04:05:05.053 回答