checklistbox数据保存在sql server 2014中 [英] checklistbox data save in sql server 2014

查看:105
本文介绍了checklistbox数据保存在sql server 2014中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我订购系统 


所以任何想法如何保存checkedlistbox检查项目sql 


示例 


在我的表格列中 


order_id,product_name,qty,price ... 


我如何在数据库中保存订单 


plzz 


我是怎么回事在数据库中保存checkedlistbox checkeditmes


解决方案

以下我写的代码示例是为了演示使用一个类将数据读入CheckedListBox的方法,该类保存了我们在表单结束事件时更新数据库表所需的信息。


数据



表格



我更新了表单关闭,您也可以在单击按钮单击事件时执行表单关闭代码。


用于填充CheckedListBox的类

命名空间CheckListBoxFromSQL_Server 
{
public class CheckListBoxItem
{
public int PrimaryKey;
public string说明;
public bool经过检查;
public override string ToString(){return Description; }
}
}

数据操作类

使用System .Collections.Generic; 
使用System.Data;
使用System.Data.SqlClient;

namespace CheckListBoxFromSQL_Server
{
public class Operations
{
///< summary>
///替换为您的SQL Server名称
///< / summary>
private string Server =" KARENS-PC" ;;
///< summary>
///数据所在的数据库,请参阅SQL_Script.sql
///< / summary>
private string Catalog =" ForumExamples" ;;
///< summary>
///用于连接数据库的连接字符串
///< / summary>
private string ConnectionString ="" ;;
///< summary>
///设置连接字符串
///< / summary>
public Operations()
{
ConnectionString =


" Data Source = {Server}; Initial Catalog = {Catalog}; Integrated Security = True" ;;
}
///< summary>
///获取要在CheckedListBox中显示的所有记录
///< / summary>
///< returns>< / returns>
public DataTable GetAll()
{
var dt = new DataTable();
using(SqlConnection cn = new SqlConnection {ConnectionString = ConnectionString})
{
using(SqlCommand cmd = new SqlCommand {Connection = cn})
{
cmd。 CommandText =" SELECT id,Code,[Description],Quantity,CheckedStatus FROM Products" ;;

cn.Open();
dt.Load(cmd.ExecuteReader());
}
}

返回dt;

}
///< summary>
///更新记录
///< / summary>
///< param name =" items">< / param>
///< remarks>
///我们更新此方法中的所有记录。您可以通过首先执行sql select + where来优化它
///,确定CheckedStatus
///是否不同然后只更新那些记录,但这取决于您。
///< / remarks>
public void Update(List< CheckListBoxItem> items)
{
using(SqlConnection cn = new SqlConnection {ConnectionString = ConnectionString})
{
using(SqlCommand cmd = new SqlCommand {Connection = cn})
{
cmd.CommandText =" UPDATE [Products] SET [CheckedStatus] = @CheckStatus WHERE id = @ id" ;;
cmd.Parameters.Add(new SqlParameter(){ParameterName =" @ CheckedStatus",SqlDbType = SqlDbType.Bit});
cmd.Parameters.Add(new SqlParameter(){ParameterName =" @ id",SqlDbType = SqlDbType.Int});

cn.Open();

foreach(项目中的CheckListBoxItem项目)
{
cmd.Parameters [" @ CheckedStatus"]。Value = item.Checked;
cmd.Parameters [" @ id"]。Value = item.PrimaryKey;
cmd.ExecuteNonQuery();
}
}
}
}
}
}

表格代码

使用System; 
使用System.Collections.Generic;
使用System.Data;
使用System.Windows.Forms;

namespace CheckListBoxFromSQL_Server
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;
FormClosing + = Form1_FormClosing;
}
///< summary>
///保存操作
///< / summary>
///< param name =" sender">< / param>
///< param name =" e">< / param>
private void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
var items = new List< CheckListBoxItem>();

for(int index = 0; index< checkedListBox1.Items.Count; index ++)
{
items.Add(new CheckListBoxItem()
{
PrimaryKey =((CheckListBoxItem)checkedListBox1.Items [index])。PrimaryKey,
Checked = checkedListBox1.GetItemChecked(index),
Description =((CheckListBoxItem)checkedListBox1.Items [index])。
});
}

var ops = new Operations();
ops.Update(items);
}
///< summary>
///从数据库表中加载CheckedListBox
///< / summary>
///< param name =" sender">< / param>
///< param name =" e">< / param>
private void Form1_Load(object sender,EventArgs e)
{
var ops = new Operations();
var dt = ops.GetAll();
int LastIndex = 0;

foreach(dt.Rows中的DataRow行)
{
checkedListBox1.Items.Add(new CheckListBoxItem()
{
Description = row.Field< ; string>(" Description"),
PrimaryKey = row.Field< int>(" id")
});

LastIndex = checkedListBox1.Items.Count - 1;
if(row.Field< bool>(" CheckedStatus"))
{
checkedListBox1.SetItemChecked(LastIndex,true);
}
}
}
}
}


如果使用VB.NET,同样适用。





hi,

i make a ordering system 

so any idea how  i  save checkedlistbox checked item in sql 

example 

in my table columns  

order_id,product_name,qty,price... 

how i can save order in database 

plzz 

how i save checkedlistbox checkeditmes in database

解决方案

The following code sample I wrote to demonstrate the method to read data into a CheckedListBox using a class that holds information we need to update the database table on form closing event.

The data

The form

I update on form closing, you could also do the form closing code in say a button click event.

Class for populating the CheckedListBox

namespace CheckListBoxFromSQL_Server
{
    public class CheckListBoxItem
    {
        public int PrimaryKey;
        public string Description;
        public bool Checked;
        public override string ToString() { return Description; }
    }
}

Data operations class

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace CheckListBoxFromSQL_Server
{
    public class Operations
    {
        /// <summary>
        /// Replace with your SQL Server name
        /// </summary>
        private string Server = "KARENS-PC";
        /// <summary>
        /// Database in which data resides, see SQL_Script.sql
        /// </summary>
        private string Catalog = "ForumExamples";
        /// <summary>
        /// Connection string for connecting to the database
        /// </summary>
        private string ConnectionString = "";
        /// <summary>
        /// Setup the connection string
        /// </summary>
        public Operations()
        {
            ConnectionString =


"Data Source={Server};Initial Catalog={Catalog};Integrated Security=True"; } /// <summary> /// Get all records to show in the CheckedListBox /// </summary> /// <returns></returns> public DataTable GetAll() { var dt = new DataTable(); using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString }) { using (SqlCommand cmd = new SqlCommand { Connection = cn }) { cmd.CommandText = "SELECT id,Code,[Description],Quantity,CheckedStatus FROM Products"; cn.Open(); dt.Load(cmd.ExecuteReader()); } } return dt; } /// <summary> /// Update records /// </summary> /// <param name="items"></param> /// <remarks> /// We update all records in this method. You could optimize it /// by first doing a sql select + where, determine if the CheckedStatus /// is different then only update those records but that is up to you. /// </remarks> public void Update(List<CheckListBoxItem> items) { using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString }) { using (SqlCommand cmd = new SqlCommand { Connection = cn }) { cmd.CommandText = "UPDATE [Products] SET [CheckedStatus] = @CheckedStatus WHERE id = @id"; cmd.Parameters.Add(new SqlParameter() { ParameterName = "@CheckedStatus", SqlDbType = SqlDbType.Bit }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@id", SqlDbType = SqlDbType.Int }); cn.Open(); foreach (CheckListBoxItem item in items) { cmd.Parameters["@CheckedStatus"].Value = item.Checked; cmd.Parameters["@id"].Value = item.PrimaryKey; cmd.ExecuteNonQuery(); } } } } } }

Form code

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace CheckListBoxFromSQL_Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FormClosing += Form1_FormClosing;
        }
        /// <summary>
        /// Save operation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            var items = new List<CheckListBoxItem>();

            for (int index = 0; index < checkedListBox1.Items.Count; index++)
            {
                items.Add(new CheckListBoxItem()
                {
                    PrimaryKey = ((CheckListBoxItem)checkedListBox1.Items[index]).PrimaryKey,
                    Checked = checkedListBox1.GetItemChecked(index),
                    Description = ((CheckListBoxItem)checkedListBox1.Items[index]).Description
                });
            }

            var ops = new Operations();
            ops.Update(items);
        }
        /// <summary>
        /// Load CheckedListBox from database table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            var ops = new Operations();
            var dt = ops.GetAll();
            int LastIndex = 0;

            foreach (DataRow row in dt.Rows)
            {
                checkedListBox1.Items.Add(new CheckListBoxItem()
                {
                    Description = row.Field<string>("Description"),
                    PrimaryKey = row.Field<int>("id")
                });

                LastIndex = checkedListBox1.Items.Count - 1;
                if (row.Field<bool>("CheckedStatus")) 
                {
                    checkedListBox1.SetItemChecked(LastIndex, true);
                }
            }
        }
    }
}

If using VB.NET the same applies.


这篇关于checklistbox数据保存在sql server 2014中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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