从 asp.net C# 网站访问数据库
Database access from within asp.net C# web sitie
我有一个要开发的 asp.net 网站,但从数据库加载数据时遇到问题。它在 C# WebForm 应用程序中运行良好,我想知道我需要做什么才能让它在 asp.net 项目中正常工作并将结果绑定到下拉列表以供选择。
try
{
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = "127.0.0.1",
InitialCatalog = "PIIMSDATA",
IntegratedSecurity = true
};
SqlConnection cs = new SqlConnection(connectionStringBuilder.ToString());
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Book1 Order by ID", cs);
}
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
//DropDownList2.DataSource = ds.Tables[0];
//DropDownList2.DataTextField = "ID";
//DropDownList2.DataValueField = "ID";
//DropDownList2.DataBind();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex + "');", true);
//MessageBox.Show(ex.Message);
}
}
您可以在 web.config 中创建连接字符串并将此参数绑定到下拉列表中
有很多方法可以做到这一点。这是我曾经尝试过的一个例子:
public string connectionString = "Data Source = YOUCANSEEONSQLSERVER; Initial Catalog = DATABASENAME; User Id = sa; Password = sqlpasswordifyouuse";
private void Valetin_Load(object sender, EventArgs e)
{
OPIDCB.ResetText();
ValetCB.ResetText();
SqlConnection sqlconn = new SqlConnection(pr.connectionString);
SqlCommand sqlselect1 = new SqlCommand("Select EmpID, EmpName from Employees.Employee where IDPosition = 'OP'", sqlconn);
sqlconn.Open();
SqlDataReader dr1 = sqlselect1.ExecuteReader();
while (dr1.Read())
{
ArrayList MyAL = new ArrayList();
ArrayList MyAL2 = new ArrayList();
MyAL.Add(dr1.GetString(0));
MyAL2.Add(dr1.GetString(1));
foreach (string s in MyAL)
foreach (string s2 in MyAL2)
{
OPIDCB.Items.Add(s + " " + s2);
}
OPIDCB.SelectedIndex = 0;
}
dr1.Close();
sqlconn.Close();
}
如果您对该代码感到困惑,可以访问此 link:What is the right way to populate a DropDownList from a database?
希望对您有所帮助。
我有一个要开发的 asp.net 网站,但从数据库加载数据时遇到问题。它在 C# WebForm 应用程序中运行良好,我想知道我需要做什么才能让它在 asp.net 项目中正常工作并将结果绑定到下拉列表以供选择。
try
{
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = "127.0.0.1",
InitialCatalog = "PIIMSDATA",
IntegratedSecurity = true
};
SqlConnection cs = new SqlConnection(connectionStringBuilder.ToString());
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Book1 Order by ID", cs);
}
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
//DropDownList2.DataSource = ds.Tables[0];
//DropDownList2.DataTextField = "ID";
//DropDownList2.DataValueField = "ID";
//DropDownList2.DataBind();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex + "');", true);
//MessageBox.Show(ex.Message);
}
}
您可以在 web.config 中创建连接字符串并将此参数绑定到下拉列表中
有很多方法可以做到这一点。这是我曾经尝试过的一个例子:
public string connectionString = "Data Source = YOUCANSEEONSQLSERVER; Initial Catalog = DATABASENAME; User Id = sa; Password = sqlpasswordifyouuse";
private void Valetin_Load(object sender, EventArgs e)
{
OPIDCB.ResetText();
ValetCB.ResetText();
SqlConnection sqlconn = new SqlConnection(pr.connectionString);
SqlCommand sqlselect1 = new SqlCommand("Select EmpID, EmpName from Employees.Employee where IDPosition = 'OP'", sqlconn);
sqlconn.Open();
SqlDataReader dr1 = sqlselect1.ExecuteReader();
while (dr1.Read())
{
ArrayList MyAL = new ArrayList();
ArrayList MyAL2 = new ArrayList();
MyAL.Add(dr1.GetString(0));
MyAL2.Add(dr1.GetString(1));
foreach (string s in MyAL)
foreach (string s2 in MyAL2)
{
OPIDCB.Items.Add(s + " " + s2);
}
OPIDCB.SelectedIndex = 0;
}
dr1.Close();
sqlconn.Close();
}
如果您对该代码感到困惑,可以访问此 link:What is the right way to populate a DropDownList from a database?
希望对您有所帮助。