Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 9th, 2012, 11:27 AM
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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)
{

}

}

}
Reply With Quote
  #2 (permalink)  
Old February 9th, 2012, 09:35 PM
Friend of Wrox
Points: 3,053, Level: 23
Points: 3,053, Level: 23 Points: 3,053, Level: 23 Points: 3,053, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Sep 2005
Location: , , .
Posts: 810
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Assuming the tables etc to remain the same in Production and Development database all you need to do is change the connection string based on the ComboBox

You can handle that like

string cs_dev = @"server=JITC-PC\GEOINT;database=DEV_GEOINT;integrated security=SSPI";

string cs_prod = @"server=JITC-PC\GEOINT;database=PROD_GEOINT;integrated security=SSPI";


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if comboBox1.text = 'Production'
{
cs = cs_prod
}
else
{
cs = cs_dev
}
}

just put appropriate syntax and it should work. All you do here is based on the Selection you assign appropriate connection string and the rest should be find

As your program matures you can have these connection string as part of config file and set them

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #3 (permalink)  
Old February 10th, 2012, 10:13 AM
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default select database based on combo box selection

Thanks for your help and quick response. Here is the code I used to fix my problems:

Code:
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)
        {
        }
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Access combo box does not work with SQL Server jerryk Access 3 July 14th, 2006 04:30 PM
dynamic upload a combo box from database on select rashmisrao JSP Basics 4 February 27th, 2006 08:41 AM
Query based on combo box selection help Elain Access 1 January 3rd, 2006 11:33 PM
Populate List Box by Combo Box Selection mmcdonal Access 2 June 15th, 2004 01:08 PM
Access Combo box with sql server jbritz Access 4 June 18th, 2003 12:33 PM



All times are GMT -4. The time now is 01:17 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.