SQL Connection
-
Can anyone tell me the best way to do this? Form1 has a listview that pulls data from a sql table and displays it in the listview. From Form1, how can i call a class that would make the SQL connection and run a query that would display my data to Form1? basically, i have a lot of Forms that individually make a SQL connection, but in the event of having to change SQL servers, i dont want to have to go into all 20 of these forms to change the SQL Connection string to point to a new server. Form1 -----
private void listfromDB() { string sqlString = "select * from table"; mySQLClass getSQLdata = new mySQLClass(); getSQLdata.SQLdata(sqlString); <...code to add data from query to listview...> }
Class1 ------public giveSQLdata(string _sqlString) { string sqlCon = "workstation id=LOCALHOST;packet size=4096;...etc"; SqlConnection sqlConnect = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand(_sqlString, sqlConnect); sqlConnect.Open(); SqlDataReader dr = cmd.ExecuteReader(); return something... }
i'm new to this and need help...i freely admit this. :) thank you so much for your help. .gonad -
Can anyone tell me the best way to do this? Form1 has a listview that pulls data from a sql table and displays it in the listview. From Form1, how can i call a class that would make the SQL connection and run a query that would display my data to Form1? basically, i have a lot of Forms that individually make a SQL connection, but in the event of having to change SQL servers, i dont want to have to go into all 20 of these forms to change the SQL Connection string to point to a new server. Form1 -----
private void listfromDB() { string sqlString = "select * from table"; mySQLClass getSQLdata = new mySQLClass(); getSQLdata.SQLdata(sqlString); <...code to add data from query to listview...> }
Class1 ------public giveSQLdata(string _sqlString) { string sqlCon = "workstation id=LOCALHOST;packet size=4096;...etc"; SqlConnection sqlConnect = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand(_sqlString, sqlConnect); sqlConnect.Open(); SqlDataReader dr = cmd.ExecuteReader(); return something... }
i'm new to this and need help...i freely admit this. :) thank you so much for your help. .gonadA number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace MyNamespace
{
public class ConfigSettings
{
public static SqlConnection DatabaseConnection;
{
get
{
string server = ConfigurationSettings.AppSettings["SqlServer"];
string database = ConfigurationSettings.AppSettings["Database"];
string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
return new SqlConnection(connectionString);
}
}
}
}The corresponding config file will look something like this:
<configuration>
<appSettings>
<add key="SqlServer" value="ServerMachineName" />
<add key="Database" value="Northwind" />
</appSettings>
</configuration>Does this help?
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.
-
A number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace MyNamespace
{
public class ConfigSettings
{
public static SqlConnection DatabaseConnection;
{
get
{
string server = ConfigurationSettings.AppSettings["SqlServer"];
string database = ConfigurationSettings.AppSettings["Database"];
string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
return new SqlConnection(connectionString);
}
}
}
}The corresponding config file will look something like this:
<configuration>
<appSettings>
<add key="SqlServer" value="ServerMachineName" />
<add key="Database" value="Northwind" />
</appSettings>
</configuration>Does this help?
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.
Colin Angus Mackay wrote: Does this help? Well, even if it doesn't help him, It sure helped me! :) It's scary how the obvious sometimes escapes me. :doh: Thanks Colin!!
Paul Lyons, CCPL
Certified Code Project Lurker -
Colin Angus Mackay wrote: Does this help? Well, even if it doesn't help him, It sure helped me! :) It's scary how the obvious sometimes escapes me. :doh: Thanks Colin!!
Paul Lyons, CCPL
Certified Code Project LurkerPaul Lyons wrote: It's scary how the obvious sometimes escapes me It happens to all of us sometimes.
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.
-
A number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace MyNamespace
{
public class ConfigSettings
{
public static SqlConnection DatabaseConnection;
{
get
{
string server = ConfigurationSettings.AppSettings["SqlServer"];
string database = ConfigurationSettings.AppSettings["Database"];
string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
return new SqlConnection(connectionString);
}
}
}
}The corresponding config file will look something like this:
<configuration>
<appSettings>
<add key="SqlServer" value="ServerMachineName" />
<add key="Database" value="Northwind" />
</appSettings>
</configuration>Does this help?
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.