[英]Error populating dropdownlist from SQL Database
因此,我已經成功地將數據連接添加到了VS項目中,現在我正嘗試使用來自數據庫的表中的數據填充下拉菜單。 但是,當我運行代碼時,它說:“登錄失敗,無法使用'Username',在此行:
cmd.Connection.Open();
這是我的C#代碼:
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;
namespace Test1234
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateDDList1();
//PopulateDDList2();
//PopulateDDList2_1();
}
public void PopulateDDList1()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [History]", new SqlConnection(ConfigurationManager.AppSettings["Connection"]));
cmd.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "Serial";
DropDownList1.DataTextField = "Serial";
DropDownList1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}//end Populate 1
}
}
這是web.config代碼:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ActioNetITInventoryConnectionString" connectionString="Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=ActioNet1234"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="Connection" value="Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=***********;Integrated Security=True"/>
</appSettings>
</configuration>
我認為由於您的連接字符串而發生錯誤。 您是否有理由不使用服務器名稱,而是使用其IP? 順便說一句:您在兩個不同的配置節中定義了兩次。
如果您使用的是IP,則必須添加端口:
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
或者,您可以使用標准方式:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
甚至可能更好:使用受信任的連接:
Server=myServerAddress;Database=myDataBase;
通常在這里看看: http : //www.connectionstrings.com/sql-server/
<appSettings>
<add key="Connection" value="Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=***********;Integrated Security=True"/>
</appSettings>
您的連接字符串包含登錄名和密碼以及Integrated Security=True
如果您嘗試使用命名的登錄名/密碼登錄,請設置Integrated Security=False
或完全不使用它
您有幾個潛在的故障點,例如:
ExecuteReader
但未實際驗證數據,即沒有Null
。 Key
和Value
時,可能會返回幾個Columns。 Serial
是有效列。 為了使您的代碼更清晰易懂,可以執行以下操作:
public IList<TModel> GetModel<T>(string query) where TModel : new()
{
var container = new List<T>();
using(var connection = new SqlConnection(dbConnection))
using(var command = new SqlCommand(query, connection))
{
connection.Open();
using(var reader = command.ExecuteReader())
while(reader.Read())
{
TModel model = new TModel();
var type = model.GetType();
var table = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToArray();
foreach(var property in type.GetProperties())
foreach(var column in table)
if(String.Compare(property.Name, column, true) == 0)
if(!reader.IsDBNull(reader.GetOrdinal(property.Name)))
property.SetValue(model, reader.GetValue(reader.GetOrdinal(property.Name)), null);
container.Add(model);
}
}
}
以下方法將構建您的模型,因此您只需執行以下操作:
drpExample.DataSource = GetModel<Person>("SELECT * FROM [History]");
drpExample.DataBind();
您確實不應該有這樣的直接查詢,您應該確實使用Parameters,但這應該可以幫助您入門。 另外,您將需要我經常應用於前端的DataKeyValue
和DataKeyText
。 您只需將值設置為模型中的相關Property
。
如我的評論所述,您不能同時提供login credentials
和狀態integrated security=true
這是一個或另一個。
Integrated Security
基本上說“使用我登錄系統時使用的當前域憑據”。
話雖如此,您可能希望它看起來像這樣:
<add key="Connection"
value="Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=ActioNet1234;"/>
盡管“連接”似乎是多余的,因為您已經具有具有相同信息的連接字符串。 您可以這樣使用:
SqlCommand cmd = new SqlCommand(
"SELECT * FROM [History]",
new SqlConnection(
ConfigurationManager.ConnectionStrings["ActioNetITInventoryConnectionString"].ConnectionString)
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.