[英]add, update and delete database data from code behind using asp.net C#
my connection string is: 我的连接字符串是:
<connectionStrings>
<add name="NorthwindConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SecurityTutorials.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
and by using the below line i'll connect to the database from code behind: 并通过使用下面的行我将从后面的代码连接到数据库:
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString)
currently i'm using below code at my .aspx page to add, update and delete the data from database. 目前我在我的.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>
i'm using ListView and by the below code i could access to all and edit all the data of the database from code behind: 我正在使用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();
}
my question is how can i do the add, edit, update and delete from the code behind and delete the from the .aspx page. 我的问题是如何从后面的代码中添加,编辑,更新和删除,并从.aspx页面中删除。 because i'm developing a template and i want to do every thing from code behind. 因为我正在开发模板,所以我想从背后的代码做所有事情。
appreciate your consideration. 感谢您的考虑。
Try This.. 尝试这个..
http://www.codeproject.com/KB/webforms/CompleteListView.aspx and http://www.codeproject.com/KB/webforms/CompleteListView.aspx和
http://www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx http://www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx
it may help you. 它可能会帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.