Hallo erstmal,
also ich bin ein Anfänger in C# und habe da so ein kleines Problem.
Ich möchte die Verbindungseinstellungen für einen MS-SQL Connect aus einer Config.ini Datei holen.
Nur irgendwie bekomme ich das nicht so ganz hin.
Ich kann nicht auf die Variablen zugreifen.
Hier mal mein Code:
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.Configuration;
using System.IO;
using System.Data.SqlClient;
namespace TestApp
{
class clsSQL
{
public string getSQL()
{
ConfDatei c_textdatei = new ConfDatei();
string fs = c_textdatei.ReadFile(@"config.ini");
int pos1 = fs.IndexOf("=") + 1;
int pos2 = fs.IndexOf("=", pos1 + 1);
int pos3 = fs.IndexOf("=", pos2 + 1);
int pos4 = fs.IndexOf("=", pos3 + 1);
int pos5 = fs.IndexOf("=", pos4 + 1);
int pos6 = fs.IndexOf("=", pos5 + 1);
int pos7 = fs.IndexOf("=", pos6 + 1);
int pos8 = fs.IndexOf("=", pos7 + 1);
string user = fs.Substring(pos1, fs.IndexOf("password") - pos1 - 2);
string pass = fs.Substring(pos2 + 1, fs.IndexOf("host") - pos2 - 3);
string host = fs.Substring(pos4 + 1, fs.IndexOf("inst") - pos4 - 3);
string inst = fs.Substring(pos5 + 1, fs.IndexOf("Trusted_Connection") - pos5 - 3);
string trust = fs.Substring(pos6 + 1, fs.IndexOf("database") - pos6 - 3);
string dbase = fs.Substring(pos7 + 1, fs.IndexOf("connection timeout") - pos7 - 3);
string timeout = fs.Substring(pos8 + 1, fs.Length - pos8 - 3);
return user;
}
private static string Database = dbase;
private static string Server = host + "\\" + inst;
private static string Password = pass;
private static string Username = user;
private static string Trust = trust;
private static string ConTime = timeout;
private static string constring = "user id=" + Username + ";"
+ "password=" + Password + ";"
+ "server=" + Server + ";"
+ "Trusted_Connection=" + Trust + ";"
+ "Database=" + Database + ";"
+ "connection timeout=" + ConTime + ";";
public static SqlConnection con = new SqlConnection(constring);
public clsSQL()
{
}
}
}
Ich hoffe, dass mir jemand helfen kann.
Gruß
workaholic
Ich hoffe, dass mir jemand helfen kann.
JA...
Ich kann nicht auf die Variablen zugreifen.
Wie willst du auch auf Variablen zugreifen, wenn die 20 Zeilen nicht mal fehlerfrei kompilieren?
Hallo workaholic
Wenn du dich mit den Grundlagen vertraut gemacht hast, hilft vielleicht auch das hier:
http://www.tsql.de/csharp/csharp-ini-file-lesen
Gruss
Coffeebean
Microsoft MVP // Me // Blog // GitHub // @Egghead // All my talks // Speakerdeck
Hallo!
Des Weiteren gibt es im Forum min. 2 fertige Klassen, welche INI-Dateien lesen und schreiben können und einen einfachen Zugriff auf die Werte ermöglichen (Suche nach "ini and klasse").
Abgesehen davon sind ini-Dateien evtl. zum üben noch OK, aber sonst sollte man entweder das [Tutorial] Konfigurationsmodell im .NET Framework verwenden, oder eigene xml-Dateien verwenden.
Nobody is perfect. I'm sad, i'm not nobody 🙁
Bitte beachte [Hinweis] Wie poste ich richtig? Punkt 1.1.1. Schau auch mal in [FAQ] Wie finde ich den Einstieg in C#?
Ini-Dateien sind out. [Tutorial] Konfigurationsmodell im .NET Framework ist in.
Da es auch um Passworte gibt, siehe auch [FAQ] DB-Password/Kennwort/Connection-String sicher speichern.