sprpxl13
Messages postés4Date d'inscriptionlundi 21 mars 2011StatutMembreDernière intervention 5 janvier 2017
-
Modifié le 23 mars 2011 à 10:00
hellskitchen
Messages postés2Date d'inscriptionmardi 5 janvier 2010StatutMembreDernière intervention23 mars 2011
-
23 mars 2011 à 10:00
Bonjour tout le monde,
Je vous expose mon problème. Je suis entrain de développer une application pour un projet d'étude. Celui ci doit être capable de retourner une liste des bases de données disponible sur le réseau.
J'avais trouvé un exemple de code (qui ma foi marche fort bien), mais lorsque j’essaie de faire la même chose, j’obtiens une erreur . "La référence d'objet n'est pas définie à une instance d'un objet." (en référence à la méthode foreach)
Si quelqu'un peut jeter un oeil au code source, cela me rendrait un grand service.
Voici le 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.Data.SqlClient;
using System.Runtime.InteropServices;
using Microsoft.SqlServer.Server;
namespace Application_Dossier_Mémoire
{
public partial class Form1 : Form
{
string _base;
string _user;
string _pass;
short SQL_HANDLE_ENV = 1;
short SQL_HANDLE_DBC = 2;
int SQL_ATTR_ODBC_VERSION = 200;
int SQL_OV_ODBC3 = 3;
short SQL_SUCCESS = 0;
short SQL_NEED_DATA = 99;
short DEFAULT_RESULT_SIZE = 1024;
string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_conn_Click(object sender, EventArgs e)
{
textBox1.Text = _user;
textBox2.Text = _pass;
textBox3.Text = _base;
SqlConnection connexion_base = new SqlConnection();
connexion_base.ConnectionString = "";
try
{
connexion_base.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (connexion_base.State == ConnectionState.Open)
{
toolStripStatusLabel1.Text = ("Copyright (2011) - Vous êtes connecté.");
btn_deco.Enabled = true;
btn_conn.Enabled = false;
btn_gene.Enabled = true;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
}
}
private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void textBox1_KeyPress(object sender, EventArgs e)
{
if (textBox1.TextLength >= 11)
{
textBox1.Clear();
textBox1.CharacterCasing = CharacterCasing.Upper;
textBox1.MaxLength = 10;
}
}
private void textBox2_KeyPress(object sender, EventArgs e)
{
if (textBox2.TextLength >= 11)
{
textBox2.Clear();
textBox2.UseSystemPasswordChar = true;
textBox2.MaxLength = 10;
}
}
private void textBox3_KeyPress(object sender, EventArgs e)
{
if (textBox3.TextLength >= 11)
{
textBox3.Clear();
textBox3.CharacterCasing = CharacterCasing.Upper;
textBox3.MaxLength = 10;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void aProposDeToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Créer par xxxxxxx');
}
private void listeDesServeursSQLlocalToolStripMenuItem_Click(object sender, EventArgs e)
{
string [] liste_serveur = null;
string txt = string.Empty;
IntPtr henv = IntPtr.Zero;
IntPtr hconn = IntPtr.Zero;
StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);
StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);
short inStringLength = (short) inString.Length;
short lenNeeded = 0;
// On récupère la liste des serveurs SQL Serveurs
try
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
{
if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
{
if (SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString, DEFAULT_RESULT_SIZE, out lenNeeded))
{
if (DEFAULT_RESULT_SIZE < lenNeeded)
{
outString.Capacity = lenNeeded;
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString, lenNeeded,out lenNeeded))
{
throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
}
}
// On récupère le nom des serveurs SQL (sans les parenthèses)
txt = outString.ToString();
int start = txt.IndexOf("{") + 1;
int len = txt.IndexOf("}") - start;
if ((start > 0) && (len > 0))
{
txt = txt.Substring(start, len);
}
else
{
txt = string.Empty;
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txt = string.Empty;
}
finally
{
if (hconn != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC,hconn);
}
if (henv != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV,hconn);
}
}
// On stocke, dans un tableau la liste des noms de serveurs
if (txt.Length > 0)
{
liste_serveur = txt.Split(",".ToCharArray());
}
// Pour chaque élément du tableau
foreach (string serveur in liste_serveur)
{
// On l'affiche dans le listView
ListViewItem ItemServer = new ListViewItem(serveur);
listView1.Items.Add(ItemServer);
ItemServer = null;
}
}
private void SQLFreeHandle(short SQL_HANDLE_DBC, IntPtr hconn)
{
throw new NotImplementedException();
}
private short SQLBrowseConnect(IntPtr hconn, StringBuilder inString, short inStringLength, StringBuilder outString, short DEFAULT_RESULT_SIZE, out short lenNeeded)
{
throw new NotImplementedException();
}
private short SQLSetEnvAttr(IntPtr henv, int SQL_ATTR_ODBC_VERSION, IntPtr intPtr, int p)
{
throw new NotImplementedException();
}
private short SQLAllocHandle(short SQL_HANDLE_ENV, IntPtr henv, out IntPtr henv_2)
{
throw new NotImplementedException();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}