0

How to resolve the adding a column to a table in SQL server

 2 years ago
source link: https://www.codeproject.com/Questions/5327588/How-to-resolve-the-adding-a-column-to-a-table-in-S
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

See more:

Expand ▼   Copy Code
<pre>using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Northwind_Sql_Data_Extraction
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
            // TODO: This line of code loads data into the 'northwindDataSet.Order_Details' table. You can move, or remove it, as needed.
            this.order_DetailsTableAdapter.Fill(this.northwindDataSet.Order_Details);
            // TODO: This line of code loads data into the 'northwindDataSet.Orders' table. You can move, or remove it, as needed.
            this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);        
        }

        private void FunctionThree()
        {
            using (SqlConnection con = new SqlConnection("Data Source=LAPTOP- CI6IN3IA\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"))
            {
               {
                    con.Open();
             string AddColumn = "ALTER TABLE[Order Details] ADD[Order Details Value] int";
                    SqlCommand sql = new SqlCommand(AddColumn, con);
                    sql.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("order detail value already created",ex.Message);
                }
                try
                {
                    string UpdateValue = "UPDATE [Order Details] SET [Order Details].[Order Details Value] = ([Order Details].Quantity * [Order Details].UnitPrice)";
                    SqlCommand insertCommand = new SqlCommand(UpdateValue, con);
                    insertCommand.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedItem.ToString())
            {
                case "Task 3":
                    FunctionThree();                   
                    break;
                default:
                    MessageBox.Show("Please select a value between 1 and 5");
                    break;
            }
        }
    }
}


What I have tried:

I'm trying to make my code for adding a new column to a table more streamlined. Although my method works, I receive an exception error indicating that the table produced must be unique. Obviously, this is due to the fact that Sql.ExecuteNonQuery causes the table to be recreated if I run the code several times, but I can't seem to find a viable solution.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK