่ช่วยดูโค้ด C# ที่เชื่อมต่อกับฐานข้อมูล sql ให้หน่อยค่ะ
หน้าฟอร์มค่ะ ตอนนีที่ทำคือคลิปที่ gridview product (อันบน) จะขึ้นมาที่ช่อง textBox เกี่ยวกับรายละเอียดสินค้าค่ะ แล้วทำการตกลงจะเข้าที่ listview และเมื่อกดสั่งจะเข้าที่ gridview order (อันล่างค่ะ) แต่พอกดสั่งแล้วขึ้น ตามรูปค่ะ ไม่รู้จะแก้ยังไง
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.IO;
using System.Drawing.Printing;
using Microsoft.VisualBasic;
namespace WinFormSheet28
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.orderTableAdapter.Fill(this.lab2DataSet1order.order);
this.productTableAdapter.Fill(this.lab2DataSetproduct.Product);
DB.connectionString = @"Data Source=DESKTOP-E3U2G8E;Initial Catalog=lab2;Integrated Security=True";
int[] amount = new int[13];
int[] num = new int[13];
listView1.Columns.Add("เลขที่ใบสั่ง", 80, HorizontalAlignment.Center);
listView1.Columns.Add("รหัสสินค้า", 80, HorizontalAlignment.Center);
listView1.Columns.Add("ชื่อสินค้า", 100, HorizontalAlignment.Center);
listView1.Columns.Add("จำนวน", 80, HorizontalAlignment.Center);
listView1.Columns.Add("ราคา", 80, HorizontalAlignment.Center);
listView1.Columns.Add("รวมเป็นเงิน", 80, HorizontalAlignment.Center);
listView1.Columns.Add("วันที่สั่ง", 80, HorizontalAlignment.Center);
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-E3U2G8ES;Initial Catalog=lab2;Integrated Security=True");
dateTimepicker1.Enabled = false;
}
public void show()
{
string sql = "select *from order";
dataGridView2.DataSource = DB.ExcecuteQuery(sql);
}
private void ButtonSave_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-E3U2G8E;Initial Catalog=lab2;Integrated Security=True");
for (int j = 0; j <= listView1.Items.Count - 1; j++)
{
string query = @"INSERT INTO order (order_id,pro_id,pro_name,amount,money,total,dateorder)
VALUES(@order_id,@pro_id,@pro_name,@amount,@money,@total,@dateorder)";
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("@order_id", txtID.Text));
sqlParams.Add(new SqlParameter("@pro_id", listView1.Items[j].SubItems[0].Text));
sqlParams.Add(new SqlParameter("@pro_name", listView1.Items[j].SubItems[1].Text));
sqlParams.Add(new SqlParameter("@amount", listView1.Items[j].SubItems[2].Text));
sqlParams.Add(new SqlParameter("@money", listView1.Items[j].SubItems[3].Text));
sqlParams.Add(new SqlParameter("@total", listView1.Items[j].SubItems[4].Text));
sqlParams.Add(new SqlParameter("@dateorder", listView1.Items[j].SubItems[5].Text));
if (DB.ExecuteNonQuery(query, sqlParams))
{
MessageBox.Show("คำสั่งซื้อเสร็จสิ้น");
show();
}
else
{
MessageBox.Show("เกิดข้อผิดพลาด ไม่สามารถสั่งสินค้าได้", "เกิดข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void MaxID()
{
int maxId = 0;
string sql = @"SELECT MAX(order_id) FROM order";
dataGridView2.DataSource = DB.ExcecuteQuery(sql);
//ClassConnection.DataReader = ClassConnection.Command.ExecuteReader();
try
{
}
catch (Exception ex)
{
maxId = 0;
}
txtID.Text = (maxId + 1).ToString("000");
textBox1.Text = txtID.Text;
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
int i = 0;
ListViewItem lvi;
int tmpProductID = 0;
string amountif = dataGridView1.CurrentRow.Cells[2].Value.ToString();
if (int.Parse(txtProQty.Text) <= int.Parse(amountif))
{
for (i = 0; i <= listView1.Items.Count - 1; i++)
{
tmpProductID = int.Parse(listView1.Items[i].SubItems[0].Text);
if (int.Parse(txtProID.Text.Trim()) == tmpProductID)
{
MessageBox.Show("รหัสสินค้าซ้ำกัน!");
txtProID.Focus();
return;
}
}
}
string[] anydata;
anydata = new string[]
{ txtID.Text,
txtProID.Text,
txtProName.Text,
txtProQty.Text,
txtProPrice.Text,
txtTotal.Text,
dateTimepicker1.Text
};
lvi = new ListViewItem(anydata);
listView1.Items.Add(lvi);
allsumcal();
amountallsumcal();
}
private void allsumcal()
{
int i = 0;
double tmptotal = 0;
for (i = 0; i <= listView1.Items.Count - 1; i++)
{
tmptotal += double.Parse(listView1.Items[i].SubItems[4].Text);
}
lblTotal.Text = tmptotal.ToString("#,##0.00");
}
private void amountallsumcal()
{
int i = 0;
int tmptotal = 0;
for (i = 0; i <= listView1.Items.Count - 1; i++)
{
tmptotal += int.Parse(listView1.Items[i].SubItems[3].Text);
}
txt.Text = tmptotal.ToString("#,##");
}
private void EnabledTrue()
{
dateTimepicker1.Enabled = true;
txtProID.Enabled = true;
txtProQty.Enabled = true;
listView1.Enabled = true;
MaxID();
}
private void EnabledFalse()
{
dateTimepicker1.Enabled = false;
txtProID.Enabled = false;
txtProQty.Enabled = false;
listView1.Enabled = false;
MaxID();
}
private void calamount()
{
double total;
total = (double.Parse(txtProPrice.Text)) * (double.Parse(txtProQty.Text));
txtTotal.Text = total.ToString("#,##0.00");
}
private void txtTotal_TextChanged(object sender, EventArgs e)
{
calamount();
}
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int i = 0;
for (i = 0; i <= listView1.SelectedItems.Count - 1; i++)
{
ListViewItem lvi;
lvi = listView1.SelectedItems[i];
listView1.Items.Remove(lvi);
}
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
txtProID.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtProName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
txtProPrice.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
}
private void txtProQty_TextChanged(object sender, EventArgs e)
{
calamount();
}
private void txt_TextChanged(object sender, EventArgs e)
{
amountallsumcal();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
Form f10 = new main();
f10.Show();
this.Hide();
}
}
}
Tag : Ms SQL Server 2014, C#
ประวัติการแก้ไข 2019-04-03 13:23:25
Date :
2019-04-03 13:22:13
By :
2165930030404449
View :
1977
Reply :
16
วิธีใช้ ชื่อ ตาราง หรือ ฟีลด์ ที่เป็นคำสงวน เมื่อมันสร้างขึ้นมาได้ มันต้องมีวิธีเรียกใช้
mssql : -- > INSERT INTO [order] ครอบด้วย [] หรือ double quote
mysql : -- > INSERT INTO `order` ครอบด้วย `` back quote
ประวัติการแก้ไข 2019-04-04 06:27:33
Date :
2019-04-04 06:26:32
By :
Chaidhanan
CRUID
เห็นก็ท้อแล้ว (เห็นอะไร?...)
Code (C#)
public static class BankRepo
{
private static string connstring = @"Data Source=SEXSEXSEX\SQLEXPRESS2012;Initial Catalog=Demo;User ID=sa;Password=admin@xxx";
public static List<BankAccount> GetList()
{
string sqlQuery = @"SELECT
BA.Id, BA.Account, BA.Branch,
U.Id, U.Name,
A.Id, A.Street, A.City,
C.Id, C.Name,
BK.Id, BK.Name,
ACT.Id, ACT.Name,
ACC.Id, ACC.Name,
M.Id, M.Name
from BankAccount BA
inner join [User] U on U.Id = BA.UserId
inner join [Address] A on A.Id = U.AddressId
inner join Currency C on C.Id = BA.CurrencyId
inner join Bank BK on BK.Id = BA.BankId
inner join AccountType ACT on ACT.Id = BA.AccountTypeId
inner join AccountCategory ACC on ACC.Id = BA.AccountCategory
inner join Manager M on M.Id = BA.ManagerId";
List<BankAccount> ret;
using (var db = new SqlConnection(connstring))
{
ret = db.Query<BankAccount>(sqlQuery,
new[]
{
typeof(BankAccount),
typeof(User),
typeof(Address),
typeof(Currency),
typeof(Bank),
typeof(AccountType),
typeof(AccountCategory),
typeof(Manager)
},
objects =>
{
var bankaccount = objects[0] as BankAccount;
var user = objects[1] as User;
var address = objects[2] as Address;
var currency = objects[3] as Currency;
var bank = objects[4] as Bank;
var accounttype = objects[5] as AccountType;
var accountcategory = objects[6] as AccountCategory;
var manager = objects[7] as Manager;
bankaccount.User = user;
bankaccount.User.Address = address;
bankaccount.Bank = bank;
bankaccount.Currency = currency;
bankaccount.AccountType = accounttype;
bankaccount.AccountCategory = accountcategory;
bankaccount.Manager = manager;
return bankaccount;
}, splitOn: "Id, Id, Id, Id, Id, Id, Id, Id").AsList();
}
return ret;
}
}
}
Date :
2019-04-06 22:10:26
By :
หน้าฮี
เผื่อจะเป็นประโยชน์สำหรับผู้ที่ผ่านไปผ่านมาบ้าง และสำหรับเจ้าของหัวข้อกระทู้
ผมกำลังจะบอกว่าลองศึกษา Dapper เป็นอีกทางเลือกหนึ่งที่ดีทีเดียว จบปัญหา SQL Query xxx
ตัวอย่างความง่ายของมั น
Code (VB.NET)
Dim repo = New Repository(Of Student)()
'แทรกระเบียน 1 ระเบียนในฐานข้อมูลจริง
Dim rowResult = repo.Insert(New Student() With {.Id = 69, .Name = "สมชาย", .FakeMatherName = "วูบวาบ"})
MsgBox(rowResult) '1-สำเร็จ 0-ล้มเหลว
Code (VB.NET)
<Table("Student")>
Public Class Student : Implements IEntity
<ExplicitKey> 'Primary key
Public Property Id As Integer
Public Property Name As String
Public Property Sex As Integer '1-ชาย 2-หญิง 3-หญิงก็ไม่ใช่ชายก็ไม่เชิง
<Computed>
Public Property Age As Integer 'แน่นอนว่าปีที่แล้วกับปีหน้าอายุคงไม่เท่ากัน
<Column("FatherName")>'ชื่อคอลัมภ์จริงบนฐานข้อมูล
Public Property FakeMatherName As String 'คอลัมภ์หลอก ประโยชน์มหาศาลถ้ามองออก
End Class
Date :
2019-04-07 08:01:08
By :
หน้าฮี
Code (C#)
{
var repo = new Repository<Student>();
// แทรกระเบียน 1 ระเบียนในฐานข้อมูลจริง
var rowResult = repo.Insert(new Student() { Id = 69, Name = "สมชาย", FakeMatherName = "วูบวาบ" });
MsgBox(rowResult); // 1-สำเร็จ 0-ล้มเหลว
}
Code (C#)
[Table("Student")]
public class Student : IEntity
{
[ExplicitKey]
public int Id { get; set; }
public string Name { get; set; }
public int Sex { get; set; } // 1-ชาย 2-หญิง 3-หญิงก็ไม่ใช่ชายก็ไม่เชิง
[Computed]
public int Age { get; set; } // แน่นอนว่าปีที่แล้วกับปีหน้าอายุคงไม่เท่ากัน
[Column("FatherName")]
public string FakeMatherName { get; set; } // คอลัมภ์หลอก ประโยชน์มหาศาลถ้ามองออก
}
งานข้างล่างนี้ผมก็ใช้ Dapper ครับผม (ทำไปเรื่อยฯจนกว่าจะเสร็จ)
Date :
2019-04-07 08:08:33
By :
หน้าฮี
เล่าบ่นให้ฟัง
คนเราต้องมีหลักยึด ถ้าไม่มีมันก็ไม่ใช่คน/มนุษย์
หลักยึดของผมมีเพียงข้อเดียวนั่นคือ มองโลกในแง่ดีเสมอ
มองโลกในแง่ดีเสมอ แล้วมันคืออะไรล่ะ ผมขยายความเพิ่มเติมเพื่อให้เข้าใจถึงแก่นแท้ของมันดังนี้
เมียเพื่อน เมียคนอื่น มันก็เหมือนเมียเรา
อันนี้เป็นความคิดเห็นส่วนตัวของผม จากหัวข้อกระทู้นี้
ถ้าผมเดาไม่ผิด คงเป็นนักศึกษา/เชึ่ยวชาญภาษาอื่นมาแล้วหัดเขียน .NET
ถ้าผมเป็นครูบาอาจารย์
ผมคงไม่สอนให้เขาเริ่มต้นหัดขึ่จักรยาน (เขียนด้วย Windows Application Forms) แต่
ผมจะสอนเขาให้เริ่มหัดขับขี่รถยนต์บีเอ็มดับบลิง (เขียน Web Application/Mobile Application) ไปเลย
--- จักรยานมาหัดขับเมื่อไหร่ก็ได้ ขี่ไม่เป็นโลกคงไม่ว่ากระไรดอกมั้ง
...
...
...
Date :
2019-04-07 09:26:53
By :
หน้าฮี
บรรยากาศหลังจากจบงานสัมมะนา (กลุ่มเพื่อนสนิท) พูดคุยกันด้วยภาษาบ้านบ้าน
เพื่อนของผม(สาว) : กูต้องลองแล้วว่ะ ไอ้หน้าฮี
ผม : กูรู้มึงลองมาทุกอย่างแล้ว ลองจนหหมอยหงอก ยกเว้น ลองมีผัวใหม่/ให้กูเอ็ด
เพื่อนของผม(สาว) : ไอ้เหี้ยปากมึงนี่...
...
...
...
Date :
2019-04-07 10:12:49
By :
หน้าฮี
Date :
2019-04-07 10:56:49
By :
หน้าฮี
บรรยากาศหลังจากจบงานสัมมะนา (กลุ่มเพื่อนสนิท) พูดคุยกันด้วยภาษาบ้านบ้าน
จาก #NO12
เพื่อนของผม(สาว) : กูไม่ให้มึงเอ็ดหรอก ไอ้หน้าฮี
ผม : กูรู้ กูก็ถามมึงอย่างนี้เสมอต้นเสมอปลาย
เพื่อนของผม(สาว) : มึงพูดกำกวมไม่เหมือนกูกำค-ย ผัวกู หมายความว่าอย่างไร
ผม : กูรู้ว่ามึงมีความเงี่ยน แต่กูมั่นใจว่ามึงเงี่ยนเป็นบางเวลา
เพื่อนของผม(สาว) : ไอ้เหี้ยกูถาม แค่แตด แต่มึง วกไปวนมาอธิบายไปถึงรูตูดกู
+++ ปึ้งปังปึ้งปัง งอนอ้อนดอ
...
...
...
ผม: มึงไม่สังเกตุหรือว่ามัน Auto Generate 3 อย่าง List --> viewAdd/viewEdit (1-แสดงข้อมูล, 2-เพิ่ม, 3-แก้ไขข้อมูล)
มึงเห็นลูกศรวนไหม หกเก้าเก้าหก เหมือนที่มึงทำกับผัวมึง และมึงให้กูดูสดสด ผ่านมือถือ อีเหี้ย
...
...
...
Date :
2019-04-07 11:33:26
By :
หน้าฮี
ผม : ตอบเพื่อนสาวไปว่า กูทำอย่างนี้ เรื่องเดียวกันมันควรจะจบในที่เดียวกัน
ทุกฯอย่าง ที่เพิ่มเติมเข้ามาก็เช่นเดียวกัน อทิเช่น เปลี่ยนภาษา Product Code <> รหัสสินค้ัา etc...
ด้วย {translate}
Code (XML)
<div class="col-12">
<div class="card">
<div class="card-status bg-azure-lightest"></div>
<div class="card-header">
<h3 class="card-title">รับสินค้า/ทะยอยรับสินค้า (Receive Products){translate}</h3>
<div class="card-options">
<a href="#" class="card-options-collapse" data-toggle="card-collapse"><i class="fe fe-chevron-up"></i></a>
<a href="#" class="card-options-fullscreen" data-toggle="card-fullscreen"><i class="fe fe-maximize"></i></a>
<a href="#" class="card-options-remove" data-toggle="card-remove"><i class="fe fe-x"></i></a>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-12">
<div class="text-right mb-0 mt-0 pt-0">
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2" onclick="testABC();"><i class="fe fe-plus mr-2"></i>เพิ่ม</button>
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2"><i class="fe fe-edit-3 mr-2"></i>แก้ไข</button>
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2"><i class="fe fe-delete mr-2"></i>ลบ</button>
</div>
<div id="divList">
<table id="example2" class="table table-striped table-bordered table-hover dt-responsive" style="width:100%;">
<thead>
<tr>
<th style="width: 120px;">รหัส</th>
<th>ชื่อ-นาม</th>
<th>หมายเหตุ</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
</tfoot>
</table>
</div>
<div id="divDetail" style="display:block;">
<div class="text-right mb-0 mt-0">
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2"><i class="fe fe-save mr-2"></i>บันทึก</button>
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2"><i class="fe fe-save mr-2"></i>บันทึกแล้วทำต่อ</button>
<button type="button" class="btn btn-pill btn-outline-info btn-sm mr-2"><i class="fe fe-skip-back mr-2"></i>ยกเลิก</button>
</div>
<form>
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="[email protected] ">
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Date :
2019-04-07 11:42:31
By :
หน้าฮี
จบที่ #16
ผมถามเพื่อนสาวของผมว่า : มึงจำได้ไหมเมื่อ 25 ปีที่แล้ว มึงกับกูรู้จักคำสั่ง
Code (C#)
Wait Windows "ข้อความอะไรก็ว่ากันไป (หน่วง Thread/ กดแป้นพิมพ์ก็คลาย)"
มึงกับกูดีใจกันฉิบหาย อึ้ง ทึ่ง เสียว ไปด้วยกัน
Code (C#)
Wait Windows "ข้อความอะไรก็ว่ากันไป (หน่วง Thread/ กดแป้นพิมพ์ก็คลาย)" Nowait
มันเป็นอะไรที่พิศดาร ล้ำลึก
...
...
..
Date :
2019-04-07 12:01:50
By :
หน้าฮี
Load balance : Server 03