Autor Beitrag
mo78
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Fr 02.03.12 15:49 
Hallo,

ich habe auf meinen Reseller Server eine MySQL Datenbank angelegt und externen zugriff erlaubt.
Nun würde ich gerne aus meiner Anwendung heraus dadrauf zugreifen.

Aber irgendwie ohne erfolg

Hier mal der Code
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data;
using MySql.Data.MySqlClient;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String connectionstring = "SERVER=xx.xx.xxx.xxx ;DATABASE=db;UID=username;PASSWORD=passwort;";
            MySqlConnection connection = new MySqlConnection(connectionstring);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader reader;
            try {
                connection.Open();
                command.CommandText = "SELECT * FROM kd;";
                reader = command.ExecuteReader();
                
                while (reader.Read()) {
                    for (int i = 0; i < reader.FieldCount; i++)
                        Console.WriteLine(reader.GetValue(i).ToString());
                    
            }
            connection.Close();

            } catch (MySqlException mysqle) {
               Console.WriteLine(mysqle);
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }

        }
    }
}


Ein verweis auf die mysql.data.dll ist eingebunden.

Es kommt leider auch keine Fehlermeldung nur eine schwarze console.

Schonmal besten Dank für jegliche hilfe

mfg
mo

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 02.03.12 16:08 
Hallo mo78,

hast du denn mal durchgesteppt (per Debugger), ob alle Zeilen einwandfrei durchlaufen werden (evtl. ist ja die Tabelle 'kd' leer und die Reader-Schleife wird nicht durchlaufen)?
mo78 Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Fr 02.03.12 16:18 
Testdaten sind in der DB drin

beim debuggen mekert er bei
ausblenden C#-Quelltext
1:
MySqlCommand command = connection.CreateCommand();					

und meldet connection.ServerThread hat eine NullReferenceException verursacht

Ich denke das muss irgendwie am connectionstring liegen
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 02.03.12 16:23 
Hmmm, probiere mal diesen Befehl erst nach dem Open zu setzen.
Und schau mal ob Open dann eine Exception wirft...
smt
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 45



BeitragVerfasst: Fr 02.03.12 16:46 
So führe ich einen SQL-Befehl durch:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
              
 MySql.Data.MySqlClient.MySqlConnection connection;
 MySql.Data.MySqlClient.MySqlCommand command;

 connection = new MySql.Data.MySqlClient.MySqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString);
 connection.Open();

 command = new MySql.Data.MySqlClient.MySqlCommand("update kunden set active=1", connection);
 command.ExecuteNonQuery();
 command.Dispose();
 connection.Close();
 connection.Dispose();


und so eine Abfrage:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
 MySql.Data.MySqlClient.MySqlConnection connection;
 MySql.Data.MySqlClient.MySqlCommand command;
 MySql.Data.MySqlClient.MySqlDataReader reader;

 connection = new MySql.Data.MySqlClient.MySqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString);
 connection.Open();

 command = new MySql.Data.MySqlClient.MySqlCommand("select id from kunden", connection);
 reader = command.ExecuteReader();
 if (reader.Read())
  {
   id = Convert.ToInt32(reader["id"]);
  }
 reader.Close();
 reader.Dispose();
 command.Dispose();
 connection.Close();
 connection.Dispose();


Das Kommando CreateCommand brauchst dafür eigentlich nicht.

VG Sascha
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 02.03.12 17:55 
Ob nun das eine oder das andere, ist egal.
Trotzdem solltest du besser die using-Anweisung verwenden (anstatt dem manuellen Dispose()), da evtl. Exceptions sonst das Dispose verhindern!