如何通过从前端提供列来向现有数据库添加新列? [英] How to add new column to the existing database by giving a column from frontend?

查看:237
本文介绍了如何通过从前端提供列来向现有数据库添加新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai Sir,



如何通过在.Net中插入前端的列来为现有数据库表添加新列?

Hai Sir,

How to add new column for the existing database table by inserting a column from the front end in .Net?

推荐答案

我不确定这是否会纠正方法。





正如Tomas建议的那样,使用ALTER TABLE并在商店程序中使用它。



此商店程序必须有三个输入参数1.列名称2.列的数据类型3.可为空或没有。



现在用这些参数装饰你的alter table查询并准备好SP。





在您的用户界面中,列名用户将给出一个文本框,一个用于数据类型的下拉列表和一个用于可为空的复选框。从UI获取所有这些值并将其传递给BA-> DA图层。



调用您创建的Store过程并将其作为输入参数传递。





Hopefull这将有效。



已编辑:

I am not sure whether this would correct approach or not.


As Tomas suggested, use ALTER TABLE and use it inside a store procedure.

This store procedure must have three input parameters 1. Column Name 2. Data type of column 3. Nullable or not.

Now decorate your alter table query with this parameters and make the SP ready.


In your UI, one text box where Column Name user will give, One Dropdown for data type and one checkbox for nullable. Get all those values from UI and pass it to BA->DA layer.

There call that Store procedure you created and passs this as input parameters.


Hopefull this will works.

Edited:
CREATE PROCEDURE MYPROC
    @COLUMNNAME  nvarchar(50),
    @DATATYPE  nvarchar(50) = 'nvarchar(50)'    
AS
BEGIN
declare @cmd nvarchar(max)
    
    set @cmd = 'ALTER TABLE TEST
    ADD' + @COLUMNNAME + ' ' + @DATATYPE

    exec sp_executesql @cmd
END;



ADO。 NET组件



使用带有参数的方法,该参数将在您的Ui中设置,并将值传递给BA和DA方法。在此DA方法中,使用以下代码。根据您的要求添加/删除。对于SQL数据类型,使用变量并根据条件填充/分配不同的SQL数据类型,并在@DATATYPE参数添加值位置使用它。




ADO.NET Component

Use a method with paramater which will be set in your Ui, and pass the values to BA and DA method. In this DA method use the below code. Add/Remove as per your requirement. For SQL data type use a variable and fill/assign different SQL data type based on condition and use it in @DATATYPE parameter add value place.

SqlConnection con = null;
try
            {
         string ConnectionString = "server=XXX;uid=sa;"+
                    "pwd=XXXX; database=northwind";
         con = new SqlConnection(ConnectionString);
         con.Open()
        SqlCommand cmdProc = new SqlCommand("MYPROC", con);
        cmdProc.CommandType = CommandType.StoredProcedure;
        cmdProc.Parameters.AddWithValue("@COLUMNNAME", "Sub4"); // Pass the text value  you got from UI here.
        cmdProc.Parameters.AddWithValue("@DATATYPE", System.Data.SqlDbType.NVarChar);      // Add proper type as per your requirement. 
        cmdProc.ExecuteNonQuery();
}


这篇关于如何通过从前端提供列来向现有数据库添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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