-
Pierwsza aplikacja w C#
brak komentarzyKod odczytany telnetem wyświetla się w Consoli. W planie jest dorobienie obsługiwana błędów z logowaniem do pliku tekstowego.
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Data.SqlClient; class TcpClientSample { public static void Main() { byte[] data = new byte[1024]; string stringData; TcpClient server; SqlConnection myConnection = new SqlConnection("user id=login;" + "password=pass;server=192.168.0.99;" + "Trusted_Connection=no;" + "database=nazwa_bazy; " + "connection timeout=30"); string Kod2 = " "; try { server = new TcpClient("192.168.0.1", port); } catch (SocketException) { Console.WriteLine("Unable to connect to server"); return; } NetworkStream ns = server.GetStream(); while(true){ int recv = ns.Read(data, 0, data.Length); stringData = Encoding.ASCII.GetString(data, 0, recv); Kod2 = stringData.Trim(); Kod2=Kod2.Remove(0,2); Console.WriteLine(Kod2); myConnection.Open(); try{ SqlCommand myCommand2 = new SqlCommand("UPDATE palety SET ID_zaladunku=(SELECT numer_zaladunku FROM lista WHERE active=1), Data_wydania=GETDATE() WHERE Kod2=@Kod2",myConnection); myCommand2.Parameters.AddWithValue("@Kod2", Kod2); myCommand2.ExecuteNonQuery(); } catch (Exception e){ Console.WriteLine(e.ToString()); } myConnection.Close(); } Console.WriteLine("Disconnecting from server..."); ns.Close(); server.Close(); } }