使用asp.net C#从代码后面添加,更新和删除数据库数据 [英] add, update and delete database data from code behind using asp.net C#

查看:67
本文介绍了使用asp.net C#从代码后面添加,更新和删除数据库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的连接字符串是:
< connectionstrings>
< add name ="NorthwindConnectionString">
connectionString ="Data Source =.\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ SecurityTutorials.mdf; Integrated Security = True; User Instance = True"
providerName ="System.Data.SqlClient"/>


并使用下面的代码行,从后面的代码连接到数据库:

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"/>


and by using the below line i''ll connect to the database from code behind:

connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString)



目前,我正在.aspx页上使用以下代码从数据库中添加,更新和删除数据.



currently i''m using below code at my .aspx page to add, update and delete the data from database.

<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>



我正在使用ListView,并通过以下代码可以访问所有代码,并可以通过下面的代码编辑数据库的所有数据:



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:

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页中删除<asp:SqlDataSource>.因为我正在开发模板,所以我想从背后的代码中做所有事情.
感谢您的考虑.



my question is how can i do the add, edit, update and delete from the code behind and delete the <asp:SqlDataSource> from the .aspx page. because i''m developing a template and i want to do every thing from code behind.
appreciate your consideration.

推荐答案

ConnectionStrings:NorthwindConnectionString%>" span> SelectCommand =" 从[产品的字母顺序列表]中选择[产品ID],[产品名称],[已停产]" span> InsertCommand 插入[产品的字母顺序列表](ProductID,ProductName,已停产)VALUES(@ ProductID,@ ProductName,@ Discontinued)" span> UpdateCommand span> DeleteCommand 从[产品的字母列表]删除,其中[ProductID] = @ ProductID" > < 插入参数 > < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 字符串" / < /insertparameters > < updateparameters > < asp:Parameter 名称 =" 类型 字符串" / < asp:Parameter 名称 =" 类型 Int32 " / < /updateparameters > < 删除参数 > < asp:Parameter 名称 =" 类型 Int32 " / < /deleteparameters >
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>



我正在使用ListView,并通过以下代码可以访问所有代码,并可以通过下面的代码编辑数据库的所有数据:



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:

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页中删除<asp:SqlDataSource>.因为我正在开发模板,所以我想从背后的代码中做所有事情.
感谢您的考虑.



my question is how can i do the add, edit, update and delete from the code behind and delete the <asp:SqlDataSource> from the .aspx page. because i''m developing a template and i want to do every thing from code behind.
appreciate your consideration.


如果您希望为ASP.NET中的CRUD(创建读取更新删除)操作开发模板,答案是ASP.NET动态数据.它具有最高级别的抽象,依赖项注入,模板等.

参考: http://www.asp.net/dynamicdata [
If you are looking to develop a template for CRUD (Create Read Update Delete) operation in ASP .NET, the answer is ASP .NET Dynamic Data. It has the extreme level of abstraction, dependency injection, templates, etc.

Ref at: http://www.asp.net/dynamicdata[^]


您可以调用方法(update(),insert(),select() ,删除sqlDataSource对象(在您的情况下为SqlDataSource1)上的sqlDataSource对象,然后从那里执行操作.像这样

You can call the methods(update(),insert(),select(),delete()) on the sqlDataSource Object(in your case its SqlDataSource1) from code behind and perform the operations from there. Like this

SqlDataSource1.Update(); or 
SqlDataSource1.Delete();


这篇关于使用asp.net C#从代码后面添加,更新和删除数据库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆