首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何连接SQL SERVER并将数据插入数据库?

如何连接SQL SERVER并将数据插入数据库?
EN

Stack Overflow用户
提问于 2013-12-05 19:22:31
回答 1查看 7.5K关注 0票数 0

我正在尝试使用C#将数据插入到SQL server2008中。在连接时,我遵循了这个solution。将数据插入数据库时,使用this。结果是数据未插入数据库。问题是Visual Studio没有给出任何错误。是什么导致了这种情况?

下面是我的代码:

代码语言:javascript
复制
DateTime Start = DateTime.Now;
        List<Point> points = new List<Point>();
        System.Data.SqlClient.SqlConnection myConnection = new SqlConnection("server=localhost;" +
                                   "Trusted_Connection=yes;" +
                                   "database=UrbanPlan; " +
                                   "connection timeout=30");
        try
        {
            myConnection.Open();
            //here is giving successfull message
            Console.WriteLine("Successful");
        }
        catch (SqlException ex)
        {
            Console.WriteLine("You failed!" + ex.Message);
        }

        string csv_file_path = @"C:\Users\Joe\Desktop\geocoding.csv";
        DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);

        int num = 0;
        while (num < 10)
        {
            double longitude = Convert.ToDouble(csvData.Rows[num][1]);
            double latitude = Convert.ToDouble(csvData.Rows[num][2]);

            int numForCheck = 0;
            while (longitude != Convert.ToDouble(csvData.Rows[numForCheck][1]) && latitude != Convert.ToDouble(csvData.Rows[numForCheck][2]))
            {
                numForCheck++;
            }

            if (numForCheck == num)
            {
                points.Add(new Point(longitude, latitude));
            }
            num++;
        }

        double eps = 1.0;
        int minPts = 2;
        List<List<Point>> clusters = GetClusters(points, eps, minPts);
        //Console.Clear();
        // print points to console
        Console.WriteLine("The {0} points are :\n", points.Count);
        foreach (Point p in points) Console.Write(" {0} ", p);
        Console.WriteLine();
        // print clusters to console
        int total = 0;
        for (int i = 0; i < clusters.Count; i++)
        {
            int count = clusters[i].Count;
            total += count;
            string plural = (count != 1) ? "s" : "";
            Console.WriteLine("\nCluster {0} consists of the following {1} point{2} :\n", i + 1, count, plural);
            foreach (Point p in clusters[i])
            {
                string sql = "INSERT INTO AntennaLocationByRegions (RegionId, Longitude, Latitude) values (@RegionId, @Longitude, @Latitude)";
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                cmd.Parameters.Add("@RegionId", SqlDbType.Int);
                cmd.Parameters["@RegionId"].Value = i + 1;
                cmd.Parameters.Add("@Longitude", SqlDbType.Float);
                cmd.Parameters["@Longitude"].Value = p.Long;
                cmd.Parameters.Add("@Latitude", SqlDbType.Float);
                cmd.Parameters["@Latitude"].Value = p.Lat;
                cmd.ExecuteNonQuery();
            }
            Console.WriteLine();
        }
EN

回答 1

Stack Overflow用户

发布于 2014-12-03 14:25:40

尝试使用此代码进行连接

代码语言:javascript
复制
SqlConnection myConnection = new SqlConnection("Database=yourdatabasename;Server=yourpcname\\SQLEXPRESS;Integrated Security=True;connect timeout = 60");
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20398673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档