如何使用SQL中保存的值填充checkedlistbox [英] How do I fill a checkedlistbox with values saved in SQL
本文介绍了如何使用SQL中保存的值填充checkedlistbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要用SQL数据库中保存的一些值填充一个复选框。
I need to fill one checkbox with some values I saved inside an SQL database.
var numfatura = _transaction.TransDocument + _transaction.TransSerial + _transaction.TransDocNumber;
这个变量我需要它,我将它保存在SQL中,如下所示:
The variable I need it this one and I saved it inside the SQL like this:
using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
{
SqlCommand command = new SqlCommand("IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250), Cont1 char(250), Cont2 char(250));", conn);
conn.Open();
SqlCommand insertCommand = new SqlCommand("INSERT INTO UXFaturas(NumFaturas, Cont1, Cont2) VALUES (numfatura, cont, cont2)", conn);
command.ExecuteNonQuery();
command.Connection = conn;
command.CommandType = System.Data.CommandType.StoredProcedure;
//command.CommandText = "USP_InsertFile";
command.Parameters.AddWithValue("@numfaturas", numfatura);
command.Parameters.AddWithValue("@cont1", cont);
command.Parameters.AddWithValue("@cont2", cont2);
//command.ExecuteNonQuery();
}
}
我的尝试:
我试图在互联网上搜索,但没有找到任何问题,我不知道应该从哪里开始。我当天回来了,但我改变了我的项目,所以这不再起作用了,但可能会有所帮助:
What I have tried:
I've tried to seach in internet but didnt find anything that was my problem and I have no clue on what should I start. I had this back in the day but I changed my project so this doesnt work anymore but may help:
string path = @"C:\Users\HP8200\Desktop\";
var files = System.IO.Directory.GetFiles(path, "*.txt");
foreach (var item in files)
{
string fileNameOnly = "";
fileNameOnly = Path.GetFileNameWithoutExtension(item);
checkedListBox1.Items.Add(fileNameOnly);
}
我正在使用c#。
I'm using c#.
推荐答案
帮助你的例子。
Hi,
An Example to help you.
private DataTable SelectData()
{
DataTable dt = null;
using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
{
SqlCommand command = new SqlCommand("SELECT myfieldtext, myfieldvalue FROM mytable", conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
}
return dt;
}
private void FillListBoxCheck()
{
DataTable dt = this.SelectData();
this.CheckBoxList1.DataTextField = "myfieldtext";
this.CheckBoxList1.DataValueField = "myfieldvalue";
this.CheckBoxList1.DataSource = dt;
this.CheckBoxList1.DataBind();
}
这篇关于如何使用SQL中保存的值填充checkedlistbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文