2012-08-11 52 views
0

从C#连接到SQL Server 2008 R2,我有一个严重的问题,因为我已经改变了一些SQL Server设置。现在,我可以不通过下面的代码连接到SQL Server:SQL插入不工作c#querybuilder

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     kamiDataSetTableAdapters.kami2TableAdapter kap = new  WindowsFormsApplication1.kamiDataSetTableAdapters.kami2TableAdapter(); 

     decimal a = Convert.ToDecimal("11.2"); 
     int b = 11; 

     kap.Insert(Convert.ToDecimal("11.2"), b, Convert.ToDecimal("11.2"), Convert.ToDecimal("11.2")); 
    } 
} 

}

(表中有四个小数和一个INT列)

但是我可以连接并插入或QueryBuilder的选择字段GUI。不幸的是,由于名誉低下,我无法上传图片。

和错误是

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

请你指导我,我的老板很生气!

预先感谢您。

+1

连接字符串在哪里?您确定SQL Server正在运行并配置为接受远程连接吗?该错误消息似乎很清楚什么是错的... – 2012-08-11 15:58:59

+0

听起来像SQL Server实例不允许远程连接,这将是我会检查的第一件事。看看你是否可以Telnet到端口1433上的SQL Server实例。 – 2012-08-11 16:05:39

+0

是啊sql正在运行,因为我可以通过querybuilder连接并执行命令。 – 2012-08-11 20:18:39

回答

0

不知道你改变了什么设置,除了连接问题之外很难说清楚。你需要从应用程序的连接字符串开始;因为它听起来像你可以使用不同的连接。

我假设远程连接可能不是它;因为你可以连接。

如果您更改了身份验证设置或数据库权限,则需要确保您的应用程序连接的任何帐户都具有适当的权限。

+0

我真的不记得我操纵了哪个设置。我刚刚关注了一些主题,并使用该命令启用了SQL身份验证。 – 2012-08-11 20:11:08

+0

你能详细解释一下你提到的正确许可吗? – 2012-08-11 20:13:01

+0

我想我有权连接到sql,因为querybuilder(defalut gui)连接并正确执行。但问题是适配器。 – 2012-08-11 20:16:09

0

两种可能性

1 /如果你使用SQL身份验证: 做出的SqlConnection,并尝试打开与您的用户名和密码 的连接,如果它适用于您的ConnectionString更改用户密码

如果您使用Windows身份验证: 您设置的连接字符串Trusted_Connection,并检查是否正常工作

2 /确保指定好SQL Server实例(服务器\ serverinstance)

+0

1)我使用sqlconnection来完成它,但“conn.open();”遇到相同的错误消息。 2)我用点来指定实例,我的意思是同一台服务器。这很重要吗? – 2012-08-11 20:08:45

+0

它不是用点而是斜线(windows认证):Server = myServerName \ theInstanceName; Database = myDataBase; Trusted_Connection = True; – 2012-08-12 00:31:47