Developers, I am new to programming and c# coding I written a code to insert the Xml data into database and it is working perfect but my requirement in code is "if table is not exists create a new table with same columns in the DataBase and insert the data " so how can I write the code ?
public void SaveXmltoDB(List<MeterReading> MeterReadingList)
{
//OpenConnection();
// CreateTableIfNotExists();
foreach (var meterReading in MeterReadingList)
{
foreach(var interval in meterReading.IntervalDatalist)
{
foreach(var reading in interval.Readinglist)
{
string command = string.Format("insert into INTERVALDATA1(SerialNumber,TimeStamp,MeterData) VALUES ({0},'{1}',{2})", meterReading.MeterName, reading.TimeStamp.ToString(), reading.RawReading);
using (SqlConnection conn = new SqlConnection("server=LAPTOP-N6V52QKD\\AKHIL5656;" +
"Trusted_Connection=yes;" +
"database=ReportServer$AKHIL5656; " +
"connection timeout=30;" + "persist security info = True;" +
"Integrated Security = SSPI;"))
{
SqlCommand myCommand = new SqlCommand(command,conn);
myCommand.CommandType = System.Data.CommandType.Text;
conn.Open();
try
{
myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
}
}
}
}
}
CloseConnection();
}
The above code is perfectly working to insert the data into my table ,In the above code how can I program If table not exists in the database create new table with same columns and insert the data?
can anyone help me on this?
Thanks,