Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create method add new column to a existing table using datatable into my database plz solve this problem
Posted
Comments
Sulabh Agarwal 4-Jul-14 5:49am    
Can you explain little bit more about your problem?
Nishant.Chauhan80 4-Jul-14 5:55am    
i want to add new column in my database table like unique id (primary key ) when import excel file to a table then automatic generate new add column to a table (unique id ) following code:-

protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/Data/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");

OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet3$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);

OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();

string con_str = @"Data Source=vaio\sqlexpress;Initial Catalog=dbemp;Integrated Security=True";

// Bulk Copy to SQL Server
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "tbdata";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/Data/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "successfully inserted";
FetchData();
newrow();

}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please select the File";
}
}
public void FetchData()
{
SqlConnection con = new SqlConnection(@"Data Source=vaio\sqlexpress;Initial Catalog=dbemp;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from tbdata", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();

}
public void newrow()
{
SqlConnection con;
SqlDataAdapter dladpt;
DataSet dldst;
DataTable mydt;

con = new SqlConnection(@"Data Source=vaio\sqlexpress;Initial Catalog=dbemp;Integrated Security=True");
dladpt = new SqlDataAdapter("select * from tbdata", con);
dldst = new DataSet();
mydt = new DataTable();
dladpt.Fill(mydt);

DataColumn dcolColumn = new DataColumn("Link", typeof(string));
mydt.Columns.Add(dcolColumn);

DataRow drowItem;

foreach (DataRow row in mydt.Columns)
{
drowItem = mydt.NewRow();
drowItem["Link"] = "secret";
mydt.Rows.Add(drowItem);
}
GridView1.DataSource = mydt;
GridView1.DataBind();
}
}

Quote:
i want to add new column in my database table like unique id (primary key ) when import excel file to a table then automatic generate new add column to a table (unique id )
Then add one Column and make it Auto-Incremented.

Refer - SQL AUTO INCREMENT Field[^]
 
Share this answer
 
Since you are using SqlBulkCopy you can create auto increment column in destination table and all add column mappings to other columns. when you select data, you can give columns names and add mappings when you call WriteToServer. for example
C#
SqlCommand cmd = new SqlCommand("select column1, columns2 from tbdata", con);

then the mappings
C#
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "tbdata";
bulkInsert.ColumnMappings.Add("column1", "column1");
bulkInsert.ColumnMappings.Add("columns2 ", "columns2");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900