我已经在其他网站上尝试和搜索了数千次,但从来没有找到一个样本或简单的代码来使用。
我已经创建了一个使用ODBC连接的应用程序C#,我还安装了MS SQL并将其配置为启用远程数据库信息共享。我想让我的数据库对每个使用我通过连接创建的应用程序的人都可用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CaseMatter
{
public partial class mainLog : Form
{
string userID, userName, userAddress, userFName, userLastName, userCity, userPassword, userTele;
public mainLog()
{
InitializeComponent();
this.Size = new Size(442, 162);
}
private void Submitbtn_Click(object sender, EventArgs e)
{
string ConnectionString =
"Data Source=xx.xx.xx.xx,1433;Initial Catalog=master;Integrated Security=SSPI;";
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name FROM example WHERE id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();
}
}}
我刚刚编辑了代码并尝试了它,我已经添加了一个try catch来处理sql异常,但当我单击submit按钮时,它仍然冻结。
希望有人能弄明白这一点。错误:“建立与SQL Server的连接时出现与网络有关的错误或特定于实例的错误。找不到服务器或服务器不可访问。请验证实例名称是否正确,以及SQL server是否配置为允许远程连接。(提供程序: TCP提供程序,错误:0-无法建立连接,因为目标计算机主动拒绝了连接。)”
发布于 2011-01-21 04:40:50
我怀疑这里发生了四件事中的一件。
如果这是一个全新的sql服务器,没有人远程连接,那么它很可能完全在SQL服务器的配置或Windows配置中。
发布于 2011-01-21 01:47:16
在您的示例中,您没有传递用户ID和密码。这仅仅是因为你不想包括你的证书吗?我假设你没有正确地列出你的IP地址。首先,我将提供可以访问您的数据库的SQL server帐户的凭据。
发布于 2011-01-21 02:04:39
我注意到您使用的是SqlConnection,它不是一个ODBC connection对象。使用SqlConnection,您实际上是通过ADO.NET进行连接。SqlConnection (包括ODBC和ADO.NET)的连接字符串语法可以在以下站点中找到:
顺便说一下,如果您连接到SQL 2005,连接字符串将如下所示:
using System.Data.SqlClient;
// trusted connection (SQL configured to use your windows ID)
string ConnectionString =
"Data Source=xxxxxxx;Initial Catalog=xxxxxx;Integrated Security=SSPI;"
// sql authentication (SQL manages its own users/pwds)
//string ConnectionString =
//"Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name from [example_autoincrement] where id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();https://stackoverflow.com/questions/4750257
复制相似问题