gpt4 book ai didi

c# - 在asp.net中从mysql数据库获取信息

转载 作者:行者123 更新时间:2023-11-29 09:07:25 25 4
gpt4 key购买 nike

我正在使用这些 using 语句-

  using System;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

这是我尝试将术语信息放入下拉列表

   protected void getAppTerm()
{
string status, y;
y = "";
string CommandText = "select term from Terms";
OdbcConnection myConnection = dbconnect();
OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);

try
{
myConnection.Open();
OdbcDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
status = reader.GetString(0);
foreach (ListItem item in ddlApplicationTerm.Items)
{
y = item.Text;
}
if (!(status == y))
{
ddlApplicationTerm.Items.Add(status);
}
}

}
catch (OdbcException ex)
{

}
finally
{
myConnection.Close();
}
}

该信息不会进入下拉列表。我也安装了所有驱动程序。

最佳答案

protected void getAppTerm()
{
string CommandText = "select term from Terms";
OdbcConnection myConnection = dbconnect();
OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);

try
{
myConnection.Open();
OdbcDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
// Currently, you're overwriting the variable "y" on every iteration
// and then just comparing the last item.text to status.

var status = reader.GetString(0);
if (!ddlApplicationTerm.Items.Contains(status)
ddlApplicationTerm.Items.Add(status);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
myConnection.Close();
}
}

关于c# - 在asp.net中从mysql数据库获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com