Autor Beitrag
Seby-Deluxe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26

Win XP
7 Enterprise
BeitragVerfasst: Mi 17.10.07 11:33 
Hallo,

ich habe eine Klasse programmiert, die eine Verbindung zur Datenbank und SQl-Abfragen verwaltet.

Leider bekomme ich einen Fehler beim Compilieren.

QoSRoboter_DatenbankVerbindung.GetconnectionStrin(string,string): Nicht alle Codfelder geben einen Wert zurück.

Es geht um die Methode GetConnectionString

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:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class QoSRoboter_DatanbankVerbindung
{
    public QoSRoboter_DatanbankVerbindung()
    {
    }
    /// <summary>
    /// Datenbank Verbindung
    /// </summary>
    /// <param name="SQLServer">Angabe des MSSQL Server</param>
    /// <param name="SQLTable">Angabe der MSSQL Tabelle</param>
    public System.Data.SqlClient.SqlConnection GetConnectionString(string SQLServer, string SQLTable)
    {
        try
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + SQLTable + ";Data Source=" + SQLServer + "";
            conn.Open();
        }
        catch(SqlException ex)
        {
            throw new Exception(ex.Message + ")(" + ex.Server + ")");
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <param name="SQLStatement">SQL-Abfrage</param>
    public System.Data.DataTable GetDataTableRead(string SQLServer, string SQLTable, string SQLStatement)
    {
        using (SqlConnection conn = GetConnectionString(SQLServer,SQLTable))
        {
            try
            {
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = SQLStatement;
                comm.CommandType = CommandType.Text;

                SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

                DataTable dt = new DataTable();

                dt.Load(reader);
                reader.Close();

                reader.Dispose();

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}


Ich hoffe mir kann einer sagen was ich falsch gemacht habt.


mfg

Seby


Moderiert von user profile iconChristian S.: Topic aus Sonstiges (C# / .NET) verschoben am Mi 17.10.2007 um 14:40
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 17.10.07 11:56 
Wie bereits in der Fehlermeldung steht, musst Du in einer Methode mit Rückgabewert eine Wert zurückgeben. Das tust Du jedoch in der ersten Methode überhaupt nicht und in der Zweiten auch nur am Ende des try-Blocks. Du musst also in der ersten im try-Block noch ein return [Irgendwas] machen und ich empfehle Dir auch, nach dem catch in beiden Methoden z.B. ein return null; zu machen.
Seby-Deluxe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26

Win XP
7 Enterprise
BeitragVerfasst: Mi 17.10.07 12:52 
Hallo,

vielen Dank für die Hilfe.

Wert zurückliefern man bin ich blöd.

Es fehlte das return conn; in der ersten Methode
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 17.10.07 12:55 
Immer diese Fragen in 17 Foren, die dann 17x von 17 verschiedenen Fachleuten beantwortet werden sollen...

Ich möchte ergänzend auf meinen Vorschlag im Entwickler-Forum hinweisen. Jürgen