| Forum: VB.NET |
Thema:
Re: SQL-Server abfragen |
Von:
Tobi Ulm (
29.07.2004 13:46) |
Hi ?,
hier ist der Socket Code (leider in C#), folgendes, wenn der port 1433 offen ist bekommst Du keine Fehlermeldung in der Textbox, ansonsten irgendwas wie Konnte keine Verbindung herstellen.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace FetchSqlServer{
public class Form1 : System.Windows.Forms.Form{
#region "datamember"
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Text.StringBuilder strBldResult = new System.Text.StringBuilder();
private System.ComponentModel.Container components = null;
#endregion
#region "ctor"
public Form1()
{
InitializeComponent();
System.Net.IPHostEntry h = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName());
this.textBox1.Text = ((IPAddress)h.AddressList.GetValue(0)).ToString();
}
#endregion
#region "dtor"
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(336, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(312, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.Validated += new System.EventHandler(this.textBox1_Validated);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 32);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(832, 608);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "richTextBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(848, 652);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e) {
this.strBldResult = new System.Text.StringBuilder();
TcpClient tcpClient = new TcpClient();
string strAdress = textBox1.Text;
System.Net.IPAddress ipAdress = IPAddress.Parse(strAdress);
try {
tcpClient.Connect(ipAdress, 1433);
}
catch (Exception eXP ){
this.strBldResult.Append("Machine: " + ipAdress + " Port: 1433\t\t " +eXP.Message.ToString());
tcpClient.Close();
}
try{
NetworkStream networkStream = tcpClient.GetStream();
if(networkStream.CanWrite){
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
networkStream.Write(sendBytes, 0, sendBytes.Length);
}
else{
this.strBldResult.Append("Machine: " + ipAdress + " Port: 1433\t\tYou cannot write data to this stream.");
tcpClient.Close();
return;
}
if(networkStream.CanRead){
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
string returndata = Encoding.UTF8.GetString(bytes);
this.strBldResult.Append("Machine: " + ipAdress + " Port: 1433\t\tThis is what the host returned to you: " + returndata);
}
}
catch (Exception eXP ) {
this.strBldResult.Append("Machine: " + ipAdress + " Port: 1433\t\t " + eXP.Message.ToString());
tcpClient.Close();
}
this.richTextBox1.Text = this.strBldResult.ToString();
}
private void textBox1_Validated(object sender, System.EventArgs e) {
System.Net.IPHostEntry h = System.Net.Dns.GetHostByName(this.textBox1.Text);
this.textBox1.Text = ((IPAddress)h.AddressList.GetValue(0)).ToString();
}
}
}
cu
Tobi
cu
Tobi
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!