从 SQL Server 数据库获取数据到标签中

And*_*e C 2 c# sql-server asp.net label

在我的 ASP.Net 网页中,我有一个标签,需要从我的数据库中检索该标签的文本。

我写入数据库没有问题,但似乎尝试再次检索数据是一项任务......

Price我需要的是从数据库中的列、表、数据与我的网页标题相同的Tickets记录或与我的网页关联的字符串中获取数据。ConcertName

我已经尝试过很多教程,但所有教程都只会给我带来错误,所以我决定在屈服并使标签静态化之前尝试最后一个地方。

如果有帮助,我已经尝试过以下方法:

第一次尝试

第二次尝试

第三次尝试

第四次尝试

Pra*_*enu 5

希望你使用c#

string MyPageTitle="MyPageTitle"; // your page title here
string myConnectionString = "connectionstring"; //you connectionstring goes here

SqlCommand cmd= new SqlCommand("select Price from Tickets where ConcertName ='" + MyPageTitle.Replace("'","''") + "'" , new SqlConnection(myConnectionString));
cmd.Connection.Open();
labelPrice.Text= cmd.ExecuteScalar().ToString(); // assign to your label
cmd.Connection.Close();
Run Code Online (Sandbox Code Playgroud)