【问题标题】:ArgumentException caught while trying to enter connection string尝试输入连接字符串时捕获 ArgumentException
【发布时间】:2014-06-12 09:14:03
【问题描述】:

我正在尝试开发一个非常简单的学生注册系统,当我构建和编译我的解决方案时,在我的连接字符串附近捕获了一个 ArgumentException。显示的异常消息是这样的:

Keyword not supported: '@datasource'.

我检查了内部异常和异常详细信息,但没有发现任何可以为我提供有关此异常背后原因的丝毫提示。

我做错了什么???是语法错误???还是我的连接字符串错误????谁能指出来吗????

这是我捕获异常的代码:

    #region Database Connection
    public SimpleEnrollmentSystem()
    {
        InitializeComponent();

        //creating a connection
        connection = new SqlConnection();
        command = connection.CreateCommand();
        connection.ConnectionString = "@DataSource = SAADMAAN;" +
            "Initial Catalog=db_student;User ID=sa;Password=***********";

        updateReady = false;
        insertReady = false;   
    }
    #endregion

我在 SQL Server 2012 中还有一个名为 db_student 的数据库。我唯一拥有的表名为 Students,其结构如下:

  CREATE TABLE Students
  (
  StudentID int unique primary key,
  FirstName varchar(50),
  LastName varchar(50),
  Gender varchar(10),
  Age int,
  Address varchar(MAX)
  )

请注意,我使用的是 Visual Studio 2012 和 MS SQL Server 2012。

【问题讨论】:

  • 看看connectionstrings.com。我假设您正在尝试连接到 SQL Server。
  • 请查看我对帖子的更新。
  • 在这种情况下,我相信 apomenes 的回答是正确的。该网站是一个很好的资源,可用于获取到各种数据源的有效连接字符串。您可以使用它来帮助您找出这些类型的问题。

标签: c# sql winforms connection-string


【解决方案1】:

尝试:

 connection.ConnectionString = @"Data Source = SAADMAAN;" +
            "Initial Catalog=db_student;User ID=sa;Password=***********";

编辑:

请务必在字符串文字中的 DataSource@ 之间留一个空格

【讨论】:

  • 不。仍然遇到同样的异常。
  • 谢谢。在“数据”和“源”之间留一个空格解决了这个问题。它现在正在工作。
【解决方案2】:

尝试使用:

 connection = new SqlConnection("data source=SAADMAAN;initial catalog=db_student;User ID=sa;Password=***********");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多