我一直在其他网站尝试和搜索数千次,但从来没有找到一个示例或简单的代码来使用。TCP/IP连接
我创建它使用ODBC连接,我也安装了MS SQL和配置为启用远程数据库信息共享的应用程序C#。我想使我的数据库可用于每个人使用我通过使用连接所做的这个应用程序。
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();
}
}
}
我刚才编辑的代码,试了一下,我添加了一个尝试catch来处理SQL异常,但是当我点击提交按钮,它仍然冻结。
希望有人知道这一点。 错误: “与SQL Server建立连接时发生网络相关或实例特定错误未找到服务器或无法访问服务器验证实例名称是否正确,并且SQL Server配置为允许远程连接(provider:TCP提供程序,error:0 - 无连接可以作出,因为目标机器积极地拒绝它。)”
请发布您收到的确切例外。看起来可能是一个安全问题,因为你没有指定登录凭证。 – 2011-01-20 17:12:37
在编译该项目之前我没有收到任何错误或者这样的问题,但是在编译之后,框架冻结了 – Ivan 2011-01-20 17:15:47
目前还不清楚发生了什么问题。你得到一个例外,不好的数据等... – JaredPar 2011-01-20 17:16:05