无法从数据库检索数据并存储为值

时间:2017-07-30 07:22:14

标签: c# sql-server

我目前正在使用gridview将数据附加到数据库的论坛项目。使用SelectedIndexChanged,它将重定向到另一个页面以在标签中显示详细信息。但是,我无法显示&没有任何具体错误。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FAQViewPost : System.Web.UI.Page
{
    string _connStr = ConfigurationManager.ConnectionStrings["WingsDrinksDbContext"].ConnectionString;
    FAQ faq = null;

    string CustQuestionCategory = null;
    string CustQuestion = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        string FAQID = Request.QueryString["FAQID"].ToString();
        Load(FAQID);
    }

    protected void Load(string FAQID)
    {
        DataTable dt = new DataTable();
        string queryStr = "SELECT * FROM [FAQ] WHERE FAQID = @FAQID ";

        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand();

        string[] arr = { queryStr };
        string allQueries = string.Join(";", arr);

        cmd.CommandText = allQueries;
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("@FAQID", FAQID);

        SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);

        sqlDa.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            lbl_category.Text = dt.Rows[0]["CustQuestionCategory"].ToString();
            lbl_question.Text = dt.Rows[0]["CustQuestion"].ToString();
        }

        conn.Close();
        conn.Dispose();
    } 
}

1 个答案:

答案 0 :(得分:0)

我不知道为什么要将SQL放入数组中。你只有一个陈述。尝试使用:

DataTable dt = new DataTable();
string queryStr = "SELECT * FROM [FAQ] WHERE FAQID = @FAQID ";
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = queryStr;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@FAQID", FAQID);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);

sqlDa.Fill(dt);

我认为您已经检查过FAQ表中是否有您要发送的ID的记录?如果使用数字ID,生活也会更安全。