我需要一些c#和sql的帮助 [英] I need to some help with c# and sql

查看:63
本文介绍了我需要一些c#和sql的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个生产管理项目我有一个ComboBoxs的问题我需要用另一个ComboBox填充一个ComboBox

这是我的代码:

< pre lang =C#> public partial class FRM_ADD_PRODUCT:表格
{
BL.CLS_PRODUCTS prd = new BL.CLS_PRODUCTS();
public FRM_ADD_PRODUCT()
{
InitializeComponent();
CMBMAINCAT_NUM.DataSource = prd.GET_ALL_CATEGORIES();
CMBMAINCAT_NAME.DataSource = prd.GET_ALL_CATEGORIES();
CMBMAINCAT_NUM.DisplayMember = CAT_NUM;
CMBMAINCAT_NUM.ValueMember = CAT_NAME;
CMBMAINCAT_NAME.DisplayMember = CAT_NAME;
CMBMAINCAT_NUM.ValueMember = CAT_NUM;
CMBSUBCAT_NUM.DataSource = prd.GET_SUB_CATEGORIES();
CMBSUBCAT_NAME.DataSource = prd.GET_SUB_CATEGORIES();
CMBSUBCAT_NUM.DisplayMember = SUB_CAT_NUM;
CMBSUBCAT_NUM.ValueMember = SUB_CAT_NAME;
CMBSUBCAT_NUM.ValueMember = CAT_NUM;
CMBSUBCAT_NAME.DisplayMember = SUB_CAT_NAME;
CMBSUBCAT_NAME.ValueMember = SUB_CAT_NUM;
CMBSUBCAT_NAME.ValueMember = CAT_NUM;

}



这是我的DATAACCSESSLAYER.cs代码

 < span class =code-keyword>使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Data.SqlClient;
使用 System.Data;

命名空间管理工具.DAL
{
class DataAccessLayer
{
SqlConnection sqlconnection;

// 此构造函数Inisialize连接对象
public DataAccessLayer()
{
sqlconnection = new SqlConnection( @ Server =。\KHALEDHOSNY; Database = Products_Management; Integrated Security = true);
}

// 打开连接的方法
public void open()
{
if (sqlconnection.State!= ConnectionState.Open)
{
sqlconnection.Open();
}
}
// 关闭连接的方法
public void 关闭()
{
if (sqlconnection.State == ConnectionState.Open)
{
sqlconnection.Close();
}
}

// 从数据库读取数据的方法

public DataTable SelectData( string stored_procedure,SqlParameter [] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
sqlcmd.Connection = sqlconnection;

if (param!= null
{
for int i = 0 ; i < param.Length; i ++)
{
sqlcmd.Parameters.Add(param [i]);
}
}
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
// 从数据库插入,更新和删除数据的方法
public void Excutecommand( string stored_Procedure,SqlParameter [] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_Procedure;
sqlcmd.Connection = sqlconnection;


if (param!= null
{
sqlcmd.Parameters.AddRange(param);
}
sqlcmd.ExecuteNonQuery();
}
}
}



这是我的CLS_PRODUCT.cs代码

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Data;
使用 System.Data.SqlClient;
命名空间管理_Productions.BL
{
class CLS_PRODUCTS
{
public DataTable GET_ALL_CATEGORIES()
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
DataTable Dt = new DataTable();
Dt = DAL.SelectData( GET_ALL_CATEGORIES);
DAL.Close();
return Dt;
}
public DataTable GET_SUB_CATEGORIES()
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
DataTable Dt = new DataTable();
Dt = DAL.SelectData( GET_SUB_CATEGORIES);
DAL.Close();
return Dt;
}
}
}





请为此我需要帮助,我想知道是否我的代码有任何问题,请知道如何解决我的问题,因为我需要快速结束我的项目。



这个链接是一张照片我的窗体用于解释我需要的东西,我想通过组合框号2(http://www14.0zz0.com/2016/02/10/11/424961203.jpg)填写组合框1号。在databsae和combobox中的表格中的2个项目在另一个数据表中有1个项目但是在两个表之间有一个主键。

和我的项目源的这个链接(管理人员 [ ^ ]



我尝试过:



我正在尝试使用combobox.SelectedIndex来制作我需要的东西,但我失败了。

抱歉我的英语不好:):)

解决方案

a。你从数据库查询的结果初始化第一个ComboBox。



a.1。将ComboBox的DataSource设置为查询结果(根据需要将查询结果转换为一组'对象),



a.2。然后根据需要设置ComboBox的'ValueMember和'DisplayMember属性。



a.3。为ComboBox实现'SelectionIndexChanged EventHandler



b。根据用户的操作...在第一个ComboBox中进行选择...



b.1。您对数据库进行另一次查询以检索第二个ComboBox的相应值,并使用a.1中提到的技术设置其值。



b.2 。实现一个事件处理程序,如a.3。



这里很大程度上取决于你的数据库的结构是什么(很多表?),以及数据库的内容(行,字段)足够小,您可以执行一个(或一系列)查询并获取UI中所需的所有数据,然后创建一个Collection来保存第二个ComboBox的值集...而不是每次在第一个ComboBox中有选择时都必须加载第二个ComboBox值。



粗略草图:

  private   int  currentSelectedIndex1 =  0 < /跨度>; 
private int currentSelectedIndex2 = 0 ;

private void comboBox1_SelectedIndexChanged(对象发​​件人,EventArgs e)
{
// 如果没有选择则忽略
if (comboBox1.SelectedIndex < 0 return ;

currentSelectedIndex1 = comboBox1.SelectedIndex;

// 获取第二个ComboBox中的值
// 按数据库查询?
// 通过绑定到数据库表?
// 使用数据集中的预编译选项?

// 使用适当的值填充第二个ComboBox
}

private void comboBox2_SelectedIndexChanged( object sender,EventArgs e)
{
// 如果没有选择则忽略
如果( comboBox2.SelectedIndex < 0 return ;

currentSelectedIndex2 = comboBox2.SelectedIndex;

// 你有一个用户选择:用它做点什么
}


我必须遗漏一些东西......这看起来太简单了:



从UI线程:

  foreach  Object  o  in  comboBox1.Items)
{
comboBox2.Items.Add(o);
}





或者,如果您需要使用后台线程中的组合框:

  private   void  BackgroundThread()
{
this .Invoke((MethodInvoker) delegate ()
{
foreach 对象 o comboBox1.Items)
{
comboBox2.Items.Add(o);
}
});
}





- Pete


I am work on a Project For Production Management I have a Problem with ComboBoxs I need to filling a ComboBox by another ComboBox
And This is my code :

public partial class FRM_ADD_PRODUCT : Form
   {
       BL.CLS_PRODUCTS prd = new BL.CLS_PRODUCTS();
       public FRM_ADD_PRODUCT()
       {
           InitializeComponent();
           CMBMAINCAT_NUM.DataSource = prd.GET_ALL_CATEGORIES();
           CMBMAINCAT_NAME.DataSource = prd.GET_ALL_CATEGORIES();
           CMBMAINCAT_NUM.DisplayMember = "CAT_NUM";
           CMBMAINCAT_NUM.ValueMember = "CAT_NAME";
           CMBMAINCAT_NAME.DisplayMember = "CAT_NAME";
           CMBMAINCAT_NUM.ValueMember = "CAT_NUM";
           CMBSUBCAT_NUM.DataSource = prd.GET_SUB_CATEGORIES();
           CMBSUBCAT_NAME.DataSource = prd.GET_SUB_CATEGORIES();
           CMBSUBCAT_NUM.DisplayMember = "SUB_CAT_NUM";
           CMBSUBCAT_NUM.ValueMember = "SUB_CAT_NAME";
           CMBSUBCAT_NUM.ValueMember = "CAT_NUM";
           CMBSUBCAT_NAME.DisplayMember = "SUB_CAT_NAME";
           CMBSUBCAT_NAME.ValueMember = "SUB_CAT_NUM";
           CMBSUBCAT_NAME.ValueMember = "CAT_NUM";

       }


and this is my code for DATAACCSESSLAYER.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace Management_Productions.DAL
{
    class DataAccessLayer
    {
        SqlConnection sqlconnection;

        //This Constructor Inisialize The connection object
        public DataAccessLayer()
        {
            sqlconnection = new SqlConnection(@"Server=.\KHALEDHOSNY; Database=Products_Management;Integrated Security=true");
        }

        //Method to open the connection
        public void open()
        {
            if (sqlconnection.State != ConnectionState.Open)
            {
                sqlconnection.Open();
            }
        }
        //method to close the connection
        public void Close()
        {
            if (sqlconnection.State == ConnectionState.Open)
            {
                sqlconnection.Close();
            }
        }

        //Method to Read Data from Database

        public DataTable SelectData(string stored_procedure, SqlParameter[] param)
        {
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandText = stored_procedure;
            sqlcmd.Connection = sqlconnection;

            if (param != null)
            {
                for (int i = 0; i < param.Length; i++)
                {
                    sqlcmd.Parameters.Add(param[i]);
                }
            }
            SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        //Method to Insert, Update, and Delete Data From Database
        public void Excutecommand(string stored_Procedure, SqlParameter[] param)
        {
            SqlCommand sqlcmd=new SqlCommand();
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandText=stored_Procedure;
            sqlcmd.Connection = sqlconnection;


            if (param != null)
            {
                sqlcmd.Parameters.AddRange(param);
            }
            sqlcmd.ExecuteNonQuery();
        }
    }
}


and this is my code for CLS_PRODUCT.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace Management_Productions.BL
{
    class CLS_PRODUCTS
    {
        public DataTable GET_ALL_CATEGORIES()
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            DataTable Dt = new DataTable();
            Dt = DAL.SelectData("GET_ALL_CATEGORIES", null);
            DAL.Close();
            return Dt;
        }
        public DataTable GET_SUB_CATEGORIES()
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            DataTable Dt = new DataTable();
            Dt = DAL.SelectData("GET_SUB_CATEGORIES", null);
            DAL.Close();
            return Dt;
        }
    }
}



Please I need help for this and I want to know if there is any problems in my codes , please I want to Know how can I solve my problem , because I need to end my project fast .

this link a pic for my windows form for explain what i need i want to fill combo box number 1 by combo box number 2( http://www14.0zz0.com/2016/02/10/11/424961203.jpg ). combobox 2 items in a table in databsae and combobox 1 items in another datatable but there is a primary key between two tables .
and this link for my project source (Management_Productions[^]

What I have tried:

I am trying to use combobox.SelectedIndex to make what i need but i have failed.
sorry for my bad english :) :)

解决方案

a. you initialize the first ComboBox from the results of a database query.

a.1. set the 'DataSource of the ComboBox to the query result (transforming the query result into a set of 'objects as required),

a.2. then set the 'ValueMember and 'DisplayMember properties of the ComboBox as needed.

a.3. implement a 'SelectionIndexChanged EventHandler for the ComboBox

b. based on the user's actions ... making a selection in the first ComboBox ...

b.1. you make another query to the database to retrieve the appropriate values for the second ComboBox, and you set its values using the techniques mentioned in a.1.

b.2. implement an event handler as in a.3.

A lot depends here on what the structure of your database is (many tables ?), and whether the database contents (rows, fields) are small enough that you can do one (or a series of) query and get all the data you'll need in the UI, and then create a Collection to hold the sets of values for the second ComboBox ... rather than having to load the second ComboBox values every time there is a selection in the first ComboBox.

A rough sketch:

private int currentSelectedIndex1 = 0;
private int currentSelectedIndex2 = 0;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // ignore if no selection
    if (comboBox1.SelectedIndex < 0) return;

    currentSelectedIndex1 = comboBox1.SelectedIndex;

    // get the values to go in the second ComboBox here
    // by database query ?
    // by binding to a database table ?
    // by using a "pre-compiled" selection from sets of "data" ?

    // fill the second ComboBox with appropriate values
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    // ignore if no selection
    if (comboBox2.SelectedIndex < 0) return;

    currentSelectedIndex2 = comboBox2.SelectedIndex;

    // you have a user choice: do something with it
}


I must be missing something... this seems too simple:

From the UI thread:

foreach (Object o in comboBox1.Items)
{
    comboBox2.Items.Add(o);
}



Or, if you're need to work with the combo boxes from a background thread:

private void BackgroundThread()
{
    this.Invoke((MethodInvoker)delegate()
    {
        foreach (Object o in comboBox1.Items)
        {
            comboBox2.Items.Add(o);
        }
    });
}



- Pete


这篇关于我需要一些c#和sql的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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