 |
|
C# .NET ถามเรื่องการดึงข้อมูลจากฐานข้อมูล Database ขึ้นมาแสดงบน Listbox ค่ะ |
|
 |
|
|
 |
 |
|
Code (C#)
private void button1_Click(object sender, System.EventArgs e)
{
// Create an instance of the ListBox.
ListBox listBox1 = new ListBox();
// Set the size and location of the ListBox.
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10,10);
// Add the ListBox to the form.
this.Controls.Add(listBox1);
// Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate();
// Loop through and add 50 items to the ListBox.
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
}
// Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate();
// Select three items from the ListBox.
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
// Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
// Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());
}
|
 |
 |
 |
 |
Date :
2012-06-04 06:52:30 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ดึงจาก Database (C#+ListView+SQL Server)
Code (C#)
System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection();
System.Data.SqlClient.SqlDataAdapter dtAdapter = new System.Data.SqlClient.SqlDataAdapter();
DataTable dt = new DataTable();
String strConnString;
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";;
objConn = new System.Data.SqlClient.SqlConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM customer";
dtAdapter = new System.Data.SqlClient.SqlDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
this.listBox1.DataSource = dt;
this.listBox1.DisplayMember = "Name";
this.listBox1.ValueMember = "CustomerID";
Download Database
Go to : (C#) ASP.NET System.Data.SqlClient - DataTable()
|
 |
 |
 |
 |
Date :
2012-06-04 06:55:37 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แถม (VB.NET+ListView+SQL Server)
Code (VB.NET)
Dim objConn As New System.Data.SqlClient.SqlConnection()
Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter()
Dim dt As New DataTable()
Dim strConnString As [String]
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New System.Data.SqlClient.SqlConnection(strConnString)
objConn.Open()
Dim strSQL As [String]
strSQL = "SELECT * FROM customer"
dtAdapter = New System.Data.SqlClient.SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.listBox1.DataSource = dt
Me.listBox1.DisplayMember = "Name"
Me.listBox1.ValueMember = "CustomerID"
Go to : DataGridView , ComboBox , ListBox : Basic in (.NET) Windows Forms Application
|
 |
 |
 |
 |
Date :
2012-06-04 06:58:37 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอขอบคุณพี่วินมากๆค่ะ 
|
 |
 |
 |
 |
Date :
2012-06-04 10:03:20 |
By :
fourto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณค่ะ ตอนนี้เจอปัญหานิดหน่อยเดี๋ยวจะลองดูค่ะ 
|
 |
 |
 |
 |
Date :
2012-06-06 23:18:36 |
By :
fourto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอรบกวนอีกรอบนะคะ โชว์ข้อมูลใน listbox ได้แล้ว แต่พอเลือกข้อมูลจาก listbox1 ไป listbox2 (ซึ่งจะมี id กับ name)
แล้วจะทำการบันทึกข้อมูลทั้งหมดใน listbox2 ลงไปเก็บในฐานข้อมูล(ข้อมูลจะลงไปหลายๆ Record ได้ยังไงคะ)
โดยต้องการบันทึกแต่ id เท่านั้นค่ะ ใช้ substring ตัดคำไม่รู้ว่าแบบนี้ถูกรึเปล่า 
แล้วมัน error ตรง add.Parameters.AddWithValue("plntid", idplnt.Items) ค่ะ
Code (C#)
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
for (int i = 0; i <= lstb_setplant.Items.Count - 1; i++)
{
string lstbplnt = lstb_setplant.Items[i].ToString();
string idplnt = lstbplnt.Substring(0, 5); //สมมติว่ามีข้อมูล 5 ตัว
string sqlComm = "INSERT INTO Test(Plnt_id) VALUES (@plntid)";
SqlCommand add = new SqlCommand(sqlComm, conn);
//add.Parameters.AddWithValue("seid", lstb_plnt.Items);
add.Parameters.AddWithValue("plntid", idplnt.Items);
int result = add.ExecuteNonQuery();
add.Dispose();
if (result < 1)
{
MessageBox.Show("บันทึกข้อมูลผิดพลาด!!");
}
else
{
MessageBox.Show("บันทึกเรียบร้อยแล้ว");
}
conn.Close();
}
|
 |
 |
 |
 |
Date :
2012-07-30 15:33:57 |
By :
fourto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ Split ตัดคำ ตอนนี้ทำได้แล้วค่ะ 
|
 |
 |
 |
 |
Date :
2012-08-01 16:01:07 |
By :
fourto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2012-08-01 20:34:24 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|