使用asp.net将dynamics 365记录插入SQL Server数据库

时间:2017-02-27 14:07:31

标签: asp.net asp.net-mvc dynamics-crm microsoft-dynamics xrm

我目前正在开展一个项目,将来自Dynamics 365网站的记录列表插入到SQL Server数据库中。但是,当我调用类文件时,当前没有插入数据库。

有人可以帮助我吗?我已经在一个省略号中放置了从crm中提取数据的代码,因为代码工作正常,所以你所阅读的内容并不长。如果需要,请告诉我。

public class ProgramPVT
{
        static void Main (string[] args)
        {
            try
            {
                ...
                int count = 0;
                int n = count;

                foreach (var item in performancevt)
                {
                    performancevt.Add(item);
                }

                var totalnumber = performancevt.Count;
                var t = totalnumber;
                var accountmanager = new string[t];
                var monthlytarget = new string[t];
                var forecast_ = new string[t];
                var actual_  = new string[t];
                var managedservices = new string[t];
                var pvtpercentage_ = new string[t];
                var mspercentage_ = new string[t];

                SqlConnection crmdbconnection = new SqlConnection("Data Source =*****;Initial Catalog=****;User Id = ******;Password = ******;");
                crmdbconnection.Open();

                foreach (var performanceitem in performancevt)
                {
                    accountmanager[n] = performanceitem.accountmanager.ToString();
                    monthlytarget[n] = performanceitem.monthlytarget.ToString();
                    forecast_[n] = performanceitem.accountmanager.ToString();
                    actual_[n] = performanceitem.accountmanager.ToString();
                    managedservices[n] = performanceitem.monthlytarget.ToString();
                    pvtpercentage_[n] = performanceitem.accountmanager.ToString();
                    mspercentage_[n] = performanceitem.accountmanager.ToString();

                    var i = 0;

                    do
                    {
                        try
                        {
                            string cmdtext = "INSERT INTO PerformanceVTarget (Account_Manager, Month_Target, Forecast, Achieved, Total_Percentage, MS_Percentage) VALUES (@Account_Manager, @Month_Target, @Forecast, @Achieved, @Total_Percentage, @MS_Percentage)";
                            using (var cmd = new SqlCommand(cmdtext, crmdbconnection))
                            {
                                {
                                    cmd.Parameters.AddWithValue("@Account_Manager", accountmanager[n]);
                                    cmd.Parameters.AddWithValue("@Month_Target", accountmanager[n]);
                                    cmd.Parameters.AddWithValue("@Forecast", accountmanager[n]);
                                    cmd.Parameters.AddWithValue("@Achieved", accountmanager[n]);
                                    cmd.Parameters.AddWithValue("@Total_Percentage", accountmanager[n]);
                                    cmd.Parameters.AddWithValue("@Account_Manager", accountmanager[n]);
                                }

                                cmd.ExecuteNonQuery();
                            }
                        }
                        catch (Exception fx)
                        {
                            Console.Write(fx);
                            Console.WriteLine("Line with ID:", n, " not inserted");
                            Console.WriteLine("Error - Press enter to Continue");
                            Console.ReadLine();
                        }

                        i++;
                    } while (i < t);
                }

                n = n + 1;                   
            }
            catch (Exception ex)
            {
                Console.Write(ex);
                Console.WriteLine("Press Enter to Continue");
                Console.ReadLine();
            }
        }
    }
}

表PerformanceVTarget

[ID] pk, int ,not null
[Report_ID] int, null
[Account_Manager] varchar(50) not null
[Month_Target] varchar(50) not null
[Forecast] varchar(50) not null
[Achieved] varchar(50) not null
[Total_Percentage] varchar(50) not null
[MS_Percentage] varchar(50) not null
[Team] varchar(50) null

1 个答案:

答案 0 :(得分:1)

你的代码执行永远不会出现在这个循环中。

foreach (var item in performancevt)
{
    performancevt.Add(item);
}

这是一个死锁,否则你应该收到错误。

此外,您没有将任何值传递给[MS_Percentage]&amp; Id(主键字段),都是非空列。