พอดีพึ่งเรียนรู้เรื่องการเขียน Code C# แล้วมีเพื่อนทักว่าเขียนโค้ดไม่ถูกเลยอยากรุ้ว่า #ผิดตรงไหน #เพราะอะไร #แบบนี้ใช้ได้ไหม รบกวนผู้รู้เรื่องนี้บอกหน่อยค่ะ
ตัอย่างโค้ด ใช้ visual Studio + SQL Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BBGunSystem
{
public partial class Product : Form
{
public Product()
{
InitializeComponent();
}
DataSet ds = new DataSet();
String Con = "Data Source=DESKTOP-9MNACAA;Initial Catalog=Roiet System;Integrated Security=True";
private void Product_Load(object sender, EventArgs e)
{
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql,Con);
sda.Fill(ds,"Product");
GridPro.DataSource = ds.Tables["Product"];
}
private void GridPro_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
else
{
GridPro.Rows[e.RowIndex].Selected = true;
DataRow dr = ds.Tables["Product"].Rows[e.RowIndex];
textBox1.Text = dr["Pid"].ToString();
textBox2.Text = dr["Pname"].ToString();
textBox3.Text = dr["Vertion"].ToString();
textBox4.Text = dr["Color"].ToString();
textBox5.Text = dr["Deteil"].ToString();
textBox6.Text = dr["Price"].ToString();
textBox7.Text = dr["Inventory"].ToString();
}
}
private void S_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='"+textBox1.Text+"'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
textBox2.Text = drs[0]["Pname"].ToString();
textBox3.Text = drs[0]["Vertion"].ToString();
textBox4.Text = drs[0]["Color"].ToString();
textBox5.Text = drs[0]["Deteil"].ToString();
textBox6.Text = drs[0]["Price"].ToString();
textBox7.Text = drs[0]["Inventory"].ToString();
}
}
private void Insert_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='"+textBox1.Text+"'");
if (drs.Length == 1)
MessageBox.Show("ข้อมูลซ้ำกัน");
else
{
DataRow dr = ds.Tables["Product"].NewRow();
dr["Pid"] = textBox1.Text;
dr["Pname"] = textBox2.Text;
dr["Vertion"] = textBox3.Text;
dr["Color"] = textBox4.Text;
dr["Deteil"] = textBox5.Text;
dr["Price"] = textBox6.Text;
dr["Inventory"] = textBox7.Text;
ds.Tables["Product"].Rows.Add(dr);
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds,"Product");
}
private void Delet_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='" + textBox1.Text + "'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
drs[0].Delete();
ds.Tables["Product"].AcceptChanges();
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds, "Product");
}
private void Up_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='" + textBox1.Text + "'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
drs[0]["Pname"] = textBox2.Text;
drs[0]["Vertion"] = textBox3.Text;
drs[0]["Color"] = textBox4.Text;
drs[0]["Deteil"] = textBox5.Text;
drs[0]["Price"] = textBox6.Text;
drs[0]["Inventory"] = textBox7.Text;
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds, "Product");
}
private void ออกจากระบบToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("คุณต้องการจะปิดโปรแกรมหรือไม่ ?", "ปิดโปรแกรม", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Application.Exit();
}
else
{
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.Hide();
Member fr = new Member();
fr.Show();
}
}
}
ถามผู้รู้เรื่องโค้ด C#
ตัอย่างโค้ด ใช้ visual Studio + SQL Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BBGunSystem
{
public partial class Product : Form
{
public Product()
{
InitializeComponent();
}
DataSet ds = new DataSet();
String Con = "Data Source=DESKTOP-9MNACAA;Initial Catalog=Roiet System;Integrated Security=True";
private void Product_Load(object sender, EventArgs e)
{
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql,Con);
sda.Fill(ds,"Product");
GridPro.DataSource = ds.Tables["Product"];
}
private void GridPro_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
else
{
GridPro.Rows[e.RowIndex].Selected = true;
DataRow dr = ds.Tables["Product"].Rows[e.RowIndex];
textBox1.Text = dr["Pid"].ToString();
textBox2.Text = dr["Pname"].ToString();
textBox3.Text = dr["Vertion"].ToString();
textBox4.Text = dr["Color"].ToString();
textBox5.Text = dr["Deteil"].ToString();
textBox6.Text = dr["Price"].ToString();
textBox7.Text = dr["Inventory"].ToString();
}
}
private void S_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='"+textBox1.Text+"'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
textBox2.Text = drs[0]["Pname"].ToString();
textBox3.Text = drs[0]["Vertion"].ToString();
textBox4.Text = drs[0]["Color"].ToString();
textBox5.Text = drs[0]["Deteil"].ToString();
textBox6.Text = drs[0]["Price"].ToString();
textBox7.Text = drs[0]["Inventory"].ToString();
}
}
private void Insert_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='"+textBox1.Text+"'");
if (drs.Length == 1)
MessageBox.Show("ข้อมูลซ้ำกัน");
else
{
DataRow dr = ds.Tables["Product"].NewRow();
dr["Pid"] = textBox1.Text;
dr["Pname"] = textBox2.Text;
dr["Vertion"] = textBox3.Text;
dr["Color"] = textBox4.Text;
dr["Deteil"] = textBox5.Text;
dr["Price"] = textBox6.Text;
dr["Inventory"] = textBox7.Text;
ds.Tables["Product"].Rows.Add(dr);
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds,"Product");
}
private void Delet_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='" + textBox1.Text + "'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
drs[0].Delete();
ds.Tables["Product"].AcceptChanges();
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds, "Product");
}
private void Up_Click(object sender, EventArgs e)
{
DataRow[] drs = ds.Tables["Product"].Select("Pid='" + textBox1.Text + "'");
if (drs.Length == 0)
MessageBox.Show("ไม่พบข้อมูล");
else
{
drs[0]["Pname"] = textBox2.Text;
drs[0]["Vertion"] = textBox3.Text;
drs[0]["Color"] = textBox4.Text;
drs[0]["Deteil"] = textBox5.Text;
drs[0]["Price"] = textBox6.Text;
drs[0]["Inventory"] = textBox7.Text;
}
GridPro.DataSource = ds.Tables["Product"];
String sql = "SELECT * FROM Product";
SqlDataAdapter sda = new SqlDataAdapter(sql, Con);
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds, "Product");
}
private void ออกจากระบบToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("คุณต้องการจะปิดโปรแกรมหรือไม่ ?", "ปิดโปรแกรม", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Application.Exit();
}
else
{
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.Hide();
Member fr = new Member();
fr.Show();
}
}
}