954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Select SQL Server database based on combo box selection

Hi,
I have a windows form with a "ComboBox" on it. The "ComboBox" has the words "Production" and "Development" in it. If the user selects "Production", I need my C# code to point to the Production database. If the user selects "Development", I need my C# code to point to the Development database. Can someone please tell me how to do this?

Sorry, I'm still not sure what to do. I am new to C#, so it is confusing. Here is my 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.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace GEOINT
{
public partial class frmForm : Form
{
public frmForm()
{
InitializeComponent();
}
//Insert using Filestream, file into SQL Server Table
private void btnInsert_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
FileInfo fi = new FileInfo(openFileDlg.FileName);
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
BinaryReader rdr = new BinaryReader(fs);
byte[] fileData = rdr.ReadBytes((int)fs.Length);
rdr.Close();
fs.Close();

string cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
string sql = "INSERT INTO Filestream_Files (Row_Guid_Col_ID, fData, fName) VALUES (default, @fData, @fName)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@fData", SqlDbType.Image, fileData.Length).Value = fileData;
cmd.Parameters.Add("@fName", SqlDbType.NVarChar).Value = fi.Name;
cmd.ExecuteNonQuery();

con.Close();
}
MessageBox.Show(fi.FullName, "Selected Document/Picture Inserted!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}

private void frmForm_Load(object sender, EventArgs e)
{

}
//Load LAST selected file to SQL Server table
private void btnRetrieve_Click(object sender, EventArgs e)
{
string cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{

con.Open();
SqlTransaction txn = con.BeginTransaction();
string sql = "SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), fName fID FROM Filestream_Files WHERE fID = (SELECT MAX(fID)FROM Filestream_Files)";
SqlCommand cmd = new SqlCommand(sql, con, txn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{

string filePath = rdr[0].ToString();
byte[] objContext = (byte[])rdr[1];
string fName = rdr[2].ToString();

SqlFileStream sfs = new SqlFileStream(filePath, objContext, System.IO.FileAccess.Read);

byte[] buffer = new byte[(int)sfs.Length];
sfs.Read(buffer, 0, buffer.Length);
sfs.Close();

//Files in the table have been written to a directory for viewing.

//string filename = @"C:\Development\Geoint\Geoint\bin\debug;
string filename = fName;

System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();

}

rdr.Close();
txn.Commit();
con.Close();
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

}

thanks,
Sharon
private void frmForm_Load(object sender, EventArgs e)
{

}
//Load LAST selected file to SQL Server table
private void btnRetrieve_Click(object sender, EventArgs e)
{
string cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";
using (SqlConnection con = new SqlConnection(cs))
{

con.Open();
SqlTransaction txn = con.BeginTransaction();
string sql = "SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), fName fID FROM Filestream_Files WHERE fID = (SELECT MAX(fID)FROM Filestream_Files)";
SqlCommand cmd = new SqlCommand(sql, con, txn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{

string filePath = rdr[0].ToString();
byte[] objContext = (byte[])rdr[1];
string fName = rdr[2].ToString();

SqlFileStream sfs = new SqlFileStream(filePath, objContext, System.IO.FileAccess.Read);

byte[] buffer = new byte[(int)sfs.Length];
sfs.Read(buffer, 0, buffer.Length);
sfs.Close();

//Files in the table have been written to a directory for viewing.

//string filename = @"C:\Development\Geoint\Geoint\bin\debug;
string filename = fName;

System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();

}

rdr.Close();
txn.Commit();
con.Close();
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

}

sharon.chapman7
Newbie Poster
7 posts since Jan 2012
Reputation Points: 9
Solved Threads: 0
 

Please use the code wrap function.

Messagebox your selection of text from the combobox with

MessageBox.Show(combobox1.text);

just to see if it displays the selected text.

Cameronsmith63
Light Poster
37 posts since May 2010
Reputation Points: 19
Solved Threads: 2
 

Database or a table? 2 different things.

Once you can messagebox your selection, you needa command like:

String a = ComboBox1.Text;

thisCommand.CommandText = "Select * from tbl_"+ a +"_whatever";
Cameronsmith63
Light Poster
37 posts since May 2010
Reputation Points: 19
Solved Threads: 2
 

Database or a table? 2 different things.

Once you can messagebox your selection, you needa command like:

String a = ComboBox1.Text;

thisCommand.CommandText = "Select * from tbl_"+ a +"_whatever";

Cameron,
This is the code that solved my problem:

string cs = "";

if (comboBox1.SelectedIndex == 0)
{
cs = @"server=JITC-PC\GEOINT;database=Production_GEOINT;integrated security=SSPI";
}
else if (comboBox1.SelectedIndex == 1)
{
cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";
}


The button code looks like this:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}

Thanks,
Sharon

sharon.chapman7
Newbie Poster
7 posts since Jan 2012
Reputation Points: 9
Solved Threads: 0
 

you dont need

comboBox1.SelectedIndex == 0


, that doesn't make the program dynamic enough. Its almost like you hard coding your selection. Try the method i did, it will help when you add more entries to that comboBox.

you can then throw this away:

if (comboBox1.SelectedIndex == 0)
{
cs = @"server=JITC-PC\GEOINT;database=Production_GEOINT;integrated security=SSPI";
} 
else if (comboBox1.SelectedIndex == 1)
{ 
cs = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";
}


and just make use of this:

cs = @"server=JITC-PC\GEOINT;database="+ComboBox1.Text+"_GEOINT;integrated security=SSPI";
Cameronsmith63
Light Poster
37 posts since May 2010
Reputation Points: 19
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: