需要帮助c#comboboxes和sql [英] Need help with c# comboboxes and sql
问题描述
您好,我现在有点困难,需要一些帮助。
我有一个组合框,可以在表单加载时从mssql表中获取数据。当我从组合框中选择一个项目时,我需要一些只读文本框来显示其他数据字段。
sql table row ---->组合框。
然后我从组合框中做出选择。
组合框更改必须用组合框的选定项触发sql查询。
查询放置数据到每个文本框。
我搜索了多天寻找解决方案但找不到任何东西。
谢谢你。
尝试了很多但没有结果。请帮助:(
使用系统;
< span class =code-keyword>使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
使用 System.Configuration;
namespace Mobiz_Takip_Destek.Arizatakip
{
public partial class ArizatakipServisgirisi :Form
{
private string arackodu;
SqlConnection连接;
string connectionString;
private object arackodu1;
private void ArizatakipServisgirisi_Load(对象发件人,EventArgs e)
{
// ----- ----------------- PERSONEL SORGUSU ------------------------------- -----------------------------
string connStr = @ 数据源= ACER \SQLEXPRESS;初始目录= mobiztakip;集成安全性='真'' 跨度>;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sorgu = 从personelBilgi中选择pAdiSoyadi 跨度>;
SqlCommand cmd = new SqlCommand(sorgu,conn);
SqlDataReader cikti = cmd.ExecuteReader();
while (cikti.Read())
{
personel1.Items.Add(cikti [ 0 跨度>]);
personel2.Items.Add(cikti [ 0 ]);
personel3.Items.Add(cikti [ 0 ]);
}
personel1.SelectedIndex = 0 ;
personel2.SelectedIndex = 0 ;
personel3.SelectedIndex = 0 ;
// ---------------- ------- PERSONEL SORGUSU ----------------------------------------- ----------------------
SqlConnection conn2 = new SqlConnection(connStr);
conn2.Open();
string sorgu2 = 选择aracKodu,kayitTarihi arizaKaydiDurum来自servisTalebi,其中arizaKaydiDurum ='True';
SqlCommand cmd2 = new SqlCommand(sorgu2,conn2);
SqlDataReader cikti2 = cmd2.ExecuteReader();
while (cikti2.Read())
{
acikTalepler.Items.Add(cikti2 [ 0 跨度>]);
}
}
public ArizatakipServisgirisi()
{
InitializeComponent ();
}
public ArizatakipServisgirisi( object arackodu1 )
{
// TODO:完成成员初始化
< span class =code-keyword> this .arackodu1 = arackodu1;
}
private void acikTalepler_SelectionIndexChanged( object sender,EventArgs e)
{
if (acikTalepler.SelectedValue!= < span class =code-string> Lütfenseçimayın...)
{
< span class =code-keyword> string secim = acikTalepler.SelectedValue.ToString();
string connStr = @ 数据源= ACER \SQLEXPRESS;初始目录= mobiztakip;集成安全性=真;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sorgu = 选择*来自kayitliaraclar WHERE aracKodu =' + secim + ';
SqlCommand cmd = new SqlCommand(sorgu,conn);
SqlDataReader cikti = cmd.ExecuteReader();
if (cikti.Read())
{
plaka.Text = cikti [ plaka]。ToString();
}
string sorgu2 = 选择*来自servisTalebi WHERE aracKodu =' + secim + '和arizaKaydiDurum = True跨度>;
SqlCommand cmd2 = new SqlCommand(sorgu2,conn);
SqlDataReader cikti2 = cmd.ExecuteReader();
if (cikti2.Read())
{
talepTarihi.Text = cikti [ plaka]。ToString();
talepPersonel.Text = cikti [ kaydiAcanPersonel]。ToString();
talepAciklama.Text = cikti [ arizaAciklama]。ToString();
}
}
}
}
}
[/ EDIT]
这是一个加载组合框并对selectedIndexChanged事件做出反应的简单示例。
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 WinFormExamples.Models;
namespace WinFormExamples.Forms
{
public 部分 类组合示例:表单
{
私人列表< car>卡洛斯;
private void ComboExample_Load(对象发件人,EventArgs e)
{
// 创建列表并添加一些数据
// 您可以通过SQL获取数据
carList = new 列表< car>();
carList.Add( new Car( 1000 , 4 , 6 , Mercedes 跨度>));
carList.Add( new Car( 1001 , 2 , 6 , 法拉利 跨度>));
carList.Add( new Car( 1002 , 2 , 12 , Jaguar 跨度>));
comboBox1.DisplayMember = CarName;
comboBox1.ValueMember = CarId;
comboBox1.DataSource = carList;
}
public ComboExample()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged( object sender,EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
Car selectedCar = carList.Where(c = > c.CarId = = Convert.ToInt64(comboBox1.SelectedValue))。First();
// 您也可以在此查询数据并使用SQL数据
txtName.Text = selectedCar.CarName;
txtDoors.Text = selectedCar.Doors.ToString();
txtCylinders.Text = selectedCar.Cylinders.ToString();
}
}
}
}
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 WinFormExamples.Models
{
public class Car
{
public long CarId { get ; set ;}
public int 门{获取; set ; }
public int Cylinders { get 跨度>; set ; }
public string CarName { get 跨度>; set ; }
public Car( long id, int 门, int 柱面,字符串名称)
{
CarId = id;
门=门;
汽缸=汽缸;
CarName = name;
}
}
} < / car > < / car > 跨度>
Hello, i am kind of stuck right now and need some help.
I have a combobox which get it's data from mssql table while form loads. There are some readonly textboxes which i need them to show other datafields when i select an item from combobox.
sql table row ----> combobox.
then i make a selection from combobox.
combobox change has to trigger an sql query with combobox's selected item.
that query puts data to each textbox.
I did search multiple days for a solution and couldn't find anything.
Thanks from now.
[EDIT - moved from comment]
Tried many things but no results. Please help :(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace Mobiz_Takip_Destek.Arizatakip
{
public partial class ArizatakipServisgirisi : Form
{
private string arackodu;
SqlConnection connection;
string connectionString;
private object arackodu1;
private void ArizatakipServisgirisi_Load(object sender, EventArgs e)
{
// ----------------------PERSONEL SORGUSU ------------------------------------------------------------
string connStr = @"Data Source=ACER\SQLEXPRESS;Initial Catalog=mobiztakip;Integrated Security='True'";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sorgu = "Select pAdiSoyadi From personelBilgi";
SqlCommand cmd = new SqlCommand(sorgu, conn);
SqlDataReader cikti = cmd.ExecuteReader();
while (cikti.Read())
{
personel1.Items.Add(cikti[0]);
personel2.Items.Add(cikti[0]);
personel3.Items.Add(cikti[0]);
}
personel1.SelectedIndex = 0;
personel2.SelectedIndex = 0;
personel3.SelectedIndex = 0;
//-----------------------PERSONEL SORGUSU ---------------------------------------------------------------
SqlConnection conn2 = new SqlConnection(connStr);
conn2.Open();
string sorgu2 = "Select aracKodu, kayitTarihi arizaKaydiDurum From servisTalebi where arizaKaydiDurum = 'True'";
SqlCommand cmd2 = new SqlCommand(sorgu2, conn2);
SqlDataReader cikti2 = cmd2.ExecuteReader();
while (cikti2.Read())
{
acikTalepler.Items.Add(cikti2[0]);
}
}
public ArizatakipServisgirisi()
{
InitializeComponent();
}
public ArizatakipServisgirisi(object arackodu1)
{
// TODO: Complete member initialization
this.arackodu1 = arackodu1;
}
private void acikTalepler_SelectionIndexChanged(object sender, EventArgs e)
{
if (acikTalepler.SelectedValue != "Lütfen seçim yapın...")
{
string secim = acikTalepler.SelectedValue.ToString();
string connStr = @"Data Source=ACER\SQLEXPRESS;Initial Catalog=mobiztakip;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sorgu = "Select * From kayitliaraclar WHERE aracKodu ='" + secim + "'";
SqlCommand cmd = new SqlCommand(sorgu, conn);
SqlDataReader cikti = cmd.ExecuteReader();
if (cikti.Read())
{
plaka.Text = cikti["plaka"].ToString();
}
string sorgu2 = "Select * From servisTalebi WHERE aracKodu ='" + secim + "' and arizaKaydiDurum = True";
SqlCommand cmd2 = new SqlCommand(sorgu2, conn);
SqlDataReader cikti2 = cmd.ExecuteReader();
if (cikti2.Read())
{
talepTarihi.Text = cikti["plaka"].ToString();
talepPersonel.Text = cikti["kaydiAcanPersonel"].ToString();
talepAciklama.Text = cikti["arizaAciklama"].ToString();
}
}
}
}
}
[/EDIT]
Here is a simple example that loads the combobox and reacts to a selectedIndexChanged event.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WinFormExamples.Models; namespace WinFormExamples.Forms { public partial class ComboExample : Form { private List<car> carList; private void ComboExample_Load(object sender, EventArgs e) { // Create the list and add some data // You can get your data via SQL instead carList = new List<car>(); carList.Add(new Car(1000, 4, 6, "Mercedes")); carList.Add(new Car(1001, 2, 6, "Ferrari")); carList.Add(new Car(1002, 2, 12, "Jaguar")); comboBox1.DisplayMember = "CarName"; comboBox1.ValueMember = "CarId"; comboBox1.DataSource = carList; } public ComboExample() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex > -1) { Car selectedCar = carList.Where(c => c.CarId == Convert.ToInt64(comboBox1.SelectedValue)).First(); // You could also query the data here and use the SQL data txtName.Text = selectedCar.CarName; txtDoors.Text = selectedCar.Doors.ToString(); txtCylinders.Text = selectedCar.Cylinders.ToString(); } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormExamples.Models { public class Car { public long CarId {get; set;} public int Doors { get; set; } public int Cylinders { get; set; } public string CarName { get; set; } public Car(long id, int doors, int cylinders, string name) { CarId = id; Doors = doors; Cylinders = cylinders; CarName = name; } } }</car></car>
这篇关于需要帮助c#comboboxes和sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!