C#根据组合框选择“开发"或“生产"数据库 [英] C# select Development or Production database based on combo box

查看:60
本文介绍了C#根据组合框选择“开发"或“生产"数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有组合框"的Windows窗体. 组合框"中包含生产"和开发"字样.如果用户选择生产",则需要C#代码指向生产数据库.如果用户选择开发",则需要C#代码指向开发"数据库.有人可以告诉我该怎么做吗?

抱歉,我仍然不确定该怎么办.我是C#的新手,所以令人困惑.这是我的代码:

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)
        {

        }
                
    }

}

推荐答案

您将两个连接字符串都放入了app.config中,并在一个在代码中创建连接的地方更改了使用的那个,基于此设置.
You put both connection strings in your app.config and you change which one you use in the one place that you create connections in your code, based on this setting.


这是我解决问题的方法:

字符串cs =";

如果(comboBox1.SelectedIndex == 0)
{
cs = @服务器= JITC-PC \ GEOINT;数据库= Production_GEOINT;集成安全性= SSPI";
}
否则(comboBox1.SelectedIndex == 1)
{
cs = @服务器= JITC-PC \ GEOINT;数据库= DEV_GEOINT;集成安全性= SSPI";
}


按钮代码如下所示:



私有void comboBox1_SelectedIndexChanged(对象发送者,EventArgs e)
{
}

谢谢,
Sharon
This is how I solved the 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


您的组合框事件

on your combobox event

<pre lang="cs">private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                  if(combobox.selectedIndex=0)
                  {
                      string select=combobox.text;
                  }
if(combobox.selectedIndex=2)
                  {
                      string select=combobox.text;
                  }
            }



然后在connectionString中使用select变量...



and then use the select variable in your connectionString...


这篇关于C#根据组合框选择“开发"或“生产"数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆