我的连接字符串是:
<connectionStrings>
<add name="NorthwindConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SecurityTutorials.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
并使用以下行我将从后面的代码连接到数据库:
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString)
目前我正在使用我的.aspx页面下面的代码来添加,更新和删除数据库中的数据。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Alphabetical list of products]"
InsertCommand = "INSERT INTO [Alphabetical list of products] (ProductID, ProductName, Discontinued)VALUES(@ProductID,@ProductName,@Discontinued)"
UpdateCommand = "UPDATE [Alphabetical list of products] SET [ProductName] = @ProductName WHERE [ProductID] = @ProductID"
DeleteCommand = "DELETE FROM [Alphabetical list of products] WHERE [ProductID]=@ProductID">
<InsertParameters>
<asp:Parameter Name="ProductID" Type="String" />
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="Discontinued" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
我正在使用ListView,通过以下代码,我可以访问所有并从后面的代码编辑数据库的所有数据:
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
{
var selectCommand = new SqlCommand("SELECT [ProductID], [ProductName], [Discontinued] FROM [Alphabetical list of products]");
var dataAdapter = new SqlDataAdapter();
var dataSet = new DataSet();
selectCommand.CommandType = CommandType.Text;
selectCommand.Connection = connection;
dataAdapter.SelectCommand = selectCommand;
connection.Open();
dataAdapter.Fill(dataSet, "myDataSet");
connection.Close();
foreach (DataRow dr in dataSet.Tables["myDataSet"].Rows)
{
dr["ProductID"] = dr["ProductID"]+"00";
}
ListView1.DataSource = dataSet;
ListView1.DataBind();
}
我的问题是如何从后面的代码中添加,编辑,更新和删除,并从.aspx页面中删除。因为我正在开发一个模板,我想用后面的代码做所有事情 感谢您的考虑。
答案 0 :(得分:3)