C# 如何从SQL到Aspx webform检索和显示数据
我是.net的新手,我试图创建一个简单的网站 我有一个多行的文本框,有固定的宽度和高度C# 如何从SQL到Aspx webform检索和显示数据,c#,asp.net,sql,C#,Asp.net,Sql,我是.net的新手,我试图创建一个简单的网站 我有一个多行的文本框,有固定的宽度和高度 <asp:TextBox ID="TextBox1" runat="server" Height="168px" TextMode="MultiLine" Width="303px"></asp:TextBox> 我还创建了一个web.config来连接我的sql数据库: <connectionStrings>
<asp:TextBox ID="TextBox1" runat="server" Height="168px" TextMode="MultiLine"
Width="303px"></asp:TextBox>
我还创建了一个web.config来连接我的sql数据库:
<connectionStrings>
<add name="TDBSConnectionString" connectionString="Data Source=local;Initial Catalog=IBSI;Persist Security Info=True;User ID=sa;Password=1"
providerName="System.Data.SqlClient" />
如何从数据库中检索数据并将内容显示在上面的文本框中。
我使用多行,因为数据不止一个。请参阅本msdn教程: 示例:
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
try
{
// Open connection to the database
string ConnectionString = "server=xeon;uid=sa;"+
"pwd=manager; database=northwind";
con = new SqlConnection(ConnectionString);
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
string CommandText = "SELECT FirstName, LastName" +
" FROM Employees" +
" WHERE (LastName LIKE @Find)";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
// Add LastName to the above defined paramter @Find
cmd.Parameters.Add(
new SqlParameter(
"@Find", // The name of the parameter to map
System.Data.SqlDbType.NVarChar, // SqlDbType values
20, // The width of the parameter
"LastName")); // The name of the source column
// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;
// Execute the query
rdr = cmd.ExecuteReader();
// Fill the list box with the values retrieved
lbFound.Items.Clear();
while(rdr.Read())
{
lbFound.Items.Add(rdr["FirstName"].ToString() +
" " + rdr["LastName"].ToString());
}
}
catch(Exception ex)
{
// Print error message
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (rdr != null)
rdr.Close();
if (con.State == ConnectionState.Open)
con.Close();
}
你试过什么?…我没有为这个创建另一个.aspx或.cs文件?还是在同一个文件上?my home.aspx只有html代码。@Bert-您可以内联编写代码或使用代码隐藏文件home.aspx.cs