So I want to create a database connection to my work database using Visual C# 2010 Express, but I come to find out that express editions don’t have remote database connections setup (wtf) so here is what I did to at least get a working connection. I don’t believe this is complete so for now this is here for reference.
I found connection strings from http://www.connectionstrings.com/sql-server-2005 and am using the following:
Connect via an IP address
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
DBMSSOCN=TCP/IP. This is how to use TCP/IP instead of Named Pipes. At the end of the Data Source is the port to use. 1433 is the default port for SQL Server.
This allows me to login via the IP address of my DB and assign what db and user to connect as. I don’t think this is too secure, but I’ll have to look at it in the future when it matters.
I am using the System.Data namespace with the following:
Declared within the base of the class
System.Data.SqlClient.SqlConnection con;
DataSet ds1 = new DataSet();
System.Data.SqlClient.SqlDataAdapter da;
Within the Form_Load
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=1.2.3.4,1433;Initial Catalog=DB;User ID=readonly;Password=ro1;";
txtPartNumber.Text = "";
//con.Open();
try
{
con.Open();
richTextBox1.Text = "Connection Success!";
}
catch(Exception exp)
{
Console.WriteLine("Connection Failed! {0}", exp);
}
//MessageBox.Show("Connection Open!");
sql = "SELECT * From Product";
//MessageBox.Show(sql);
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "Product"); //DataAdapter populated called Product
richTextBox1.Text = "PART NUMBER NAME\n ===========================================";
//DisplayResults();
con.Close();
// MessageBox.Show("Connection Closed!");
con.Dispose();