开发者

TCP/IP Connection

开发者 https://www.devze.com 2023-02-05 14:03 出处:网络
I have been trying and searched thousands of times in other sites but never found a sample or simple code to use.

I have been trying and searched thousands of times in other sites but never found a sample or simple code to use.

I've created an application C# which uses an ODBC connection, i have also MS SQL installed and configured to enable remoting database information sharing. I want to make my database available to everyone uses this application i have made by using a connection.

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();

    }
}

}

I have just edited the code and tried it, i have added a try catch to handle sql exceptions but it still freezes when i click on submit button.

Hopefully someone figures this out. ERROR: "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. (pr开发者_开发百科ovider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)"


I suspect there are one of four things going on here.

  1. The SQL Server is misconfigured and isn't responding to tcp connections. The easiest way to trouble shoot this is to use SQL Management Studio to connect. If you can't connect this way then you'll have to look at the sql server service setup. Also, make sure that the SQL Browser service is running on the server.

  2. The SQL Server is configured to use a different TCP port than the one you are trying. Remove the ",1433" from your connection string and try again. SQL Browser should respond back with the actual port that sql server is listening on.

  3. The SQL Server has a firewall in place that is blocking remote connections. Temporarily turn off the firewall and see if you can connect. If you can, then configure the firewall correctly and turn it back on.

  4. Your local box has some type of firewall that is blocking outgoing connections on that port. Try turning yours off to see if this helps. If so, configure it correctly and turn it back on.


If this is a brand new sql server that no one has remotely connected to then it's very likely to be entirely within the configuration of SQL server or the Windows configuration.


In your example you are not passing the USER ID and Password. Is that just because you didnt want to include your credentials? I assume not as you properly XXX out your IP address. I would start by supplying credentials of a SQL server account that can access your database.


I noticed that you're using SqlConnection which is not an ODBC connection object. With SqlConnection you're actually connecting via ADO.NET. The connection string syntax for SqlConnection (both ODBC and ADO.NET) can be found on the following site:

http://connectionstrings.com

By the way, if you're connecting to SQL 2005, the connection string would look like this:

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();


Ok, this is a strange one to check but just make sure the time and date is accurate on client and server. SSPI requires a maximum gap of 5 minutes between each

0

精彩评论

暂无评论...
验证码 换一张
取 消