如果表不存在,如何编写c#代码在db中创建一个新表并插入数据 [英] how to write c# code if table not exists create a new table in db and insert the data

查看:69
本文介绍了如果表不存在,如何编写c#代码在db中创建一个新表并插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员,我是编程和c#编码的新手我编写了一个代码,用于将xml数据插入到数据库中,它工作正常但我必须在代码中插入我的需求"如果表不存在则创建一个具有相同列的新表并插入
数据"那么如何编写代码? 

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 I have to insert my requirement in code "if table is not exists create a new table with same columns 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?

谢谢,

sai teja akhil madivada

sai teja akhil madivada

推荐答案

以上代码完全可以将数据插入到我的表中,在上面的代码中如何编程如果数据库中不存在表创建具有相同表的新表列和插入数据?

就我而言,你不能用C#代码来做,或者就我而言你不应该这么做。 您需要转到MS SQL Server并使用 手动创建表。使用T-SQL脚本或使用MS SQL Server Management Studio在表格中创建

As far as I am concerned,  you can't do it from C# code or you shouldn't as far as I am concerned. You will need to go to the MS SQL Server and manually create the table using  a T-SQL script or using MS SQL Server Management Studio to create the table.


这篇关于如果表不存在,如何编写c#代码在db中创建一个新表并插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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