【问题标题】:C# Unhandled Argument ExceptionC# 未处理的参数异常
【发布时间】:2023-03-11 10:10:01
【问题描述】:

我收到一个错误,这是我的代码。

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

感谢您的帮助。

【问题讨论】:

  • 异常的有趣部分是您在单击“查看详细信息”时看到的内容。 ArgumentException(通常)发生在由于将错误数据作为参数传入而发生某些事情时。但是,交集的东西通常在 Message 属性中(如果它不为 null,则在 InnerException 属性中)。通过检查 $exception 伪变量,您可以在 Watch 窗口中查看当前异常。哦,您可以将异常详细信息复制/粘贴为文本。
  • 异常图像没有用。此外,它们是不赞成投票的一个很好的理由(请参阅this)。所以,请用文本替换它。

标签: c# sql-server database-connection


【解决方案1】:

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

conn.ConnectionString = "Server=DESKTOP-7AN9GF5;Database=Velocity;Trusted_Connection=true";

【讨论】:

  • 是的。我删除了代码中的另一个 SqlConnection。我只使用了你所说的 ConnectionString,它对我有用。谢谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多