嘿我正在尝试将数据库信息添加到我的网页中。这是我正在尝试的代码。
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
namespace DatabaseAddDemo
{
public partial class Content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["key"] != null)
{
try
{
SqlConnection sqlConn = new SqlConnection(@"Data Source=officedev1;Initial Catalog=TestDatabase;User ID=sa;Password=Password11;pooling='true';Connect Timeout=3000; Max Pool Size=200;MultipleActiveResultSets='true'");
SqlCommand cmdPreWork = new SqlCommand(@"select * from CKEditor_Table where @ID = @key", sqlConn);
string key = Request.QueryString["keys"].ToString();
contentLiteral.Text = key;
cmdPreWork.Parameters.Add("@Information", SqlDbType.Char).Value = key;
Console.WriteLine(contentLiteral);
SqlDataAdapter daPreWork = new SqlDataAdapter(cmdPreWork);
DataTable dtPreWork = new DataTable();
daPreWork.Fill(dtPreWork);
Grid.DataSource = dtPreWork;
Grid.DataBind();
}
catch (Exception ex)
{
lblError.Text = "Could not open connection";
}
}
}
}
}
每当我尝试显示信息时,都会收到lblError文本,告诉我无法打开连接。我不知道该怎么做。请帮忙。
答案 0 :(得分:2)
您应该能够 DEBUG ,如评论中所述。但是在您的代码中,您的选择查询需要[Key]参数,并且您正在传递[Information]参数。
将其更改如下:
SqlCommand cmdPreWork = new SqlCommand(@"select * from CKEditor_Table
where @ID = @key", sqlConn);
cmdPreWork.Parameters.Add("@key", SqlDbType.Char).Value = key;