繁体   English   中英

尝试连接到SQL Server中的本地数据库时,ServerVersion抛出“ System.InvalidOperationException”

[英]ServerVersion throws 'System.InvalidOperationException' when trying to connect to local database in SQL Server

我正在尝试练习ADO.net框架,而我却刚开始。 我正在尝试使用以下字符串连接到本地数据库:

connectionString="Server=localhost;Database=AdventureWorks2012; Integrated Security=True;"

但是,当我尝试使用创建的按钮访问数据库时,出现此错误

有趣的是,在使用按钮之前,一切都很好,正如我在调试和调试之后看到的那样

conn.Open(); 

(ServerVersion尚未引发异常,而是返回版本“ 11.00.3153”)

除了连接字符串,我非常确定代码正确,因此我认为问题与配置有关。 有任何想法吗?

代码中断的事件是:

private void buttonGetEmployee_Click(object sender, EventArgs e)
    {
        try
        {
            DataLayer.Employees es = new DataLayer.Employees();
            DataLayer.Employee employee = es.GetEmployee(int.Parse(textBoxEID.Text));

            textBoxFName.Text = employee.FirstName;
            textBoxLName.Text = employee.LastName;
            textBoxDName.Text = employee.DepartmentName;

        }
        catch { }

我刚刚发现了问题。

public class Employees{...}

相关命令

cmd.CommandText = @"MyQuery";

定义不正确,不包含预期的属性,因此,对于问题中显示的事件,它返回了错误的对象,

buttonGetEmployee_Click(object sender, EventArgs e)

这会导致您看到的错误,该错误似乎与ServerVersion有关,但实际上表明在调用事件以使用对象时发生错误

Employees es

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM