Local Database editing in C#
Hey, I am working on a project for my uncle where I have to keep a record of his stuff in his store. I have created a local database in C#. The problem is that I want to give the user the ability to add a column when he wants by just clicking a button. Can anyone please tell me what could be the lines of code for just adding a column?? I can do the rest i-e button click check and all other stuff. I need only the code lines (Query) to add a column. Thanks
Yogesh TyagiPosted Jun 26, 2014, 5:17 AM
using System.Data;
using System.Data.SqlClient;
class ExecuteDDL {
public static void Main() {
SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = "CREATE TABLE MyEmployee (" + " ID int CONSTRAINT PK_Persons PRIMARY KEY," + " FirstName nvarchar(15) NOT NULL," + " LastName nvarchar(15) NOT NULL," + " DateOfBirth datetime" + ")";
mySqlConnection.Open(); Console.WriteLine("Creating MyEmployee table"); int result = mySqlCommand.ExecuteNonQuery();
Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result); mySqlCommand.CommandText = "ALTER TABLE MyEmployee " + "ADD EmployerID nchar(5) CONSTRAINT FK_Persons_Customers " + "REFERENCES Employee(ID)"; Console.WriteLine("Altering MyEmployee table");
result = mySqlCommand.ExecuteNonQuery(); Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result); mySqlCommand.CommandText = "DROP TABLE MyEmployee"; Console.WriteLine("Dropping MyEmployee table");
result = mySqlCommand.ExecuteNonQuery(); Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result); mySqlConnection.Close();
}
}
Yogesh TyagiPosted Jun 26, 2014, 5:16 AM
http://www.asp.net/web-forms/tutorials/data-access/advanced-data-access-scenarios/adding-additional-datatable-columns-cs
http://stackoverflow.com/questions/6426828/how-to-add-new-column-with-value-to-the-existing-datatable
http://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx