【问题标题】:Get specify value from Query in WebForms从 WebForms 中的 Query 获取指定值
【发布时间】:2017-03-09 18:46:59
【问题描述】:

我在从查询中获取指定值时遇到问题。我的 GridView 收到一行,但是当我尝试读取该值以将其放入某个变量时,它说我没有任何值。

else if (Request.QueryString.AllKeys.Contains("a"))
{
    String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = ?";
    connection.OPEN();

    OleDbCommand sql_s = new OleDbCommand(sql_user, polaczenie);

    sql_s.Parameters.add("?", OleDbType.Integer).Value = Request.QueryString["a"];

    OleDbDataAdapter sqlDa = new OleDbDataAdapter(sql_s);

    DataTable dt = new DataTable();
    sqlDa.Fill(dt);

    GRID_VIEW1.DataSource = dt;

    GRID_VIEW1.DataBind();

    OleDbDataReader customer = sql_s.ExecuteReader();

    // i want to get SHORT_NAME so that one below i think dosent work

    // but any way it make me an Exception of any value not found but on GridView it shows me clearly one ROW
    learerLabel.Text = (String) cmd.ExecuteScalar();

    connection.Close();
}

我在很多方面都这样做了,比如一些会话或阅读器,但我现在不记得我的所有尝试,但它总是说我的查询没有价值。我究竟做错了什么?谢谢

【问题讨论】:

  • 尝试用"@p1"替换Parameters.add("?", OleDbType.Integer)中的"?"

标签: c# visual-studio-2010 webforms executequery


【解决方案1】:

最好的方法是做一个类似的类:

    public class MyClass
{
    public int Id { get; set; }
    public string ShortName { get; set; }
    public string Name { get; set; }
    public int Regon { get; set; }
}

接下来要从您的查询中获取数据,试试这个:

            CustomClass item = new CustomClass();

        String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @id";

        using (var conn = new OleDbConnection("connectionString"))
        {
            conn.Open();
            using (var cmd = new OleDbCommand(sql_user, conn))
            {
                cmd.Parameters.Add("@id", OleDbType.Integer).Value = Request.QueryString["a"];
                cmd.Prepare();

                var reader = cmd.ExecuteReader();

                if(reader.Read())
                {
                    item.Id = (int)reader["ID"];
                    item.ShortName = reader["SHORT_NAME"].ToString();
                    item.Name= reader["NAME"].ToString();
                    item.Regon = (int)reader["REGON"];
                }
                conn.Close();
            }

        }

编辑: 如果你不想使用它,你需要改变这条线:

OleDbDataReader customer = sql_s.ExecuteReader();

到:

            OleDbDataReader customer;
        var dr = sql_s.ExecuteReader();

        if (dr.Read())
            customer = dr;

试试这个,然后写它工作

【讨论】:

    【解决方案2】:

    试试这个,把参数名换成@ID,参考下面的链接获取msdn的参数传递文档

    SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @ID
    sql_s.Parameters.add("@ID", OleDbType.Integer).Value = Request.QueryString["a"];
    

    有关传递参数的更多详细信息,请参见下面的 msdn https://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.110).aspx

    【讨论】:

    • 谢谢,我会试试的。但是你能告诉我从这个查询(行)执行该值的最佳方法是什么?
    • 我不确定我是否正确理解了您的问题,但您可以使用数据阅读器或数据适配器。如果您的查询只返回几行,您可以使用数据阅读器,否则我建议使用数据适配器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多