C#未处理的参数异常

时间:2018-08-13 03:02:29

标签: c# sql-server database-connection

我遇到错误,这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dbgen
{
       static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            using(SqlConnection conn = new SqlConnection())
            {
                    conn.ConnectionString = "Server=DESKTOP-7AN9GF5;Database=Velocity;Trusted_Connection=true";
                    // using the code here...
                    conn.ConnectionString = "connection_string";
                    conn.Open();

                using (SqlConnection connection = new SqlConnection(
                "Integrated Security=SSPI;Initial Catalog=Northwind"))
                {
                    connection.Open();
                    // Pool A is created.
                }

                using (SqlConnection connection = new SqlConnection(
                "Integrated Security=SSPI;Initial Catalog=pubs"))
                {
                    connection.Open();
                    // Pool B is created because the connection strings differ.
                }

                using (SqlConnection connection = new SqlConnection(
                "Integrated Security=SSPI;Initial Catalog=Northwind"))
                {
                    connection.Open();
                    // The connection string matches pool A.
                }
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new mainform());
        }
}
       }

我正在使用Visual Studio 2013将数据库连接到SQL Server Express上的Velocity [数据库名称]。这是错误的屏幕截图。

ERROR SCREENSHOT

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您必须为“ conn.ConnectionString”分配正确的连接字符串。 “ connection_string”不是连接字符串,应类似于“ Server = xxx; Database = xxx;”。

答案 1 :(得分:0)

您的连接字符串格式错误。 你为什么不使用:

body{
    position: absolute;
    padding:0;
    margin: 0;
    top:0;
    left:0;
    width: 100%;
    height: 100%;
}