سلام.
این کدها از نوع Text هستند که برای آپدیت، ذخیره ، حذف اطلاعات و بازگشت دادن اطلاعات از پایگاه داده ""SQL Server"" کاربرد دارن.
یک Database جدید در پایگاه داده ایجاد کنید و همچنین یک جدول به نام Table1 و فیلدهای Code ، FirstName ، LastName وAddress
فرم برنامه:
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;
using System.Data.SqlClient;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection con;
private static string GetConnectionString()
{
return "SERVER = Server name; DATABASE = Database name; Integrated Security = True";
}
private void ClearTextBoxs()
{
foreach (Control item in groupBox1.Controls)
if (item is TextBox)
item.ResetText();
}
private void Form1_Load(object sender, EventArgs e)
{
using (con = new SqlConnection(GetConnectionString()))
using (SqlDataAdapter sda1 = new SqlDataAdapter(new SqlCommand()))
using (DataTable dt = new DataTable())
{
sda1.SelectCommand.Connection = con;
sda1.SelectCommand.CommandText = "SELECT Code AS N'کد' ,FirstName AS N'نام' ,LastName AS N'نام خانوادگی' ,Address AS N'آدرس' FROM Table1";
sda1.Fill(dt);
dataGridView1.DataSource = dt;
//تنظیم سایز ستونهای دیتا گرید ویو
dataGridView1.Columns["کد"].Width = 75;
dataGridView1.Columns["نام"].Width = 100;
dataGridView1.Columns["نام خانوادگی"].Width = 100;
dataGridView1.Columns["آدرس"].Width = 300;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (con = new SqlConnection(GetConnectionString()))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into Table1(Code, FirstName, LastName, Address)values(@Code, @FirstName, @LastName, @Address)";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Code", txtCode.Text.Trim());
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
cmd.ExecuteNonQuery();
}
con.Close();
}
MessageBox.Show("با موفقیت ثبت گردید");
Form1_Load(null, null);
ClearTextBoxs();
}
private void btnDelete_Click(object sender, EventArgs e)
{
using (con = new SqlConnection(GetConnectionString()))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("DELETE from Table1 WHERE Code = " + txtCode.Text.Trim() + "", con))
{
cmd.ExecuteNonQuery();
}
con.Close();
}
Form1_Load(null, null);
}
private void btnEdit_Click(object sender, EventArgs e)
{
using (con = new SqlConnection(GetConnectionString()))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "UPDATE Table1 SET FirstName = @FirstName, LastName = @LastName, Address = @Address WHERE Code = " + txtCode.Text.Trim() + "";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Code", txtCode.Text.Trim());
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
cmd.ExecuteNonQuery();
}
con.Close();
}
MessageBox.Show("با موفقیت ویرایش گردید");
Form1_Load(null, null);
ClearTextBoxs();
}
private void btnSearch_Click(object sender, EventArgs e)
{
SqlDataAdapter sda = null;
DataTable dt = null;
try
{
using (con = new SqlConnection(GetConnectionString()))
{
sda = new SqlDataAdapter(new SqlCommand());
sda.SelectCommand.CommandText = "SELECT Code AS N'کد' ,FirstName AS N'نام' ,LastName AS N'نام خانوادگی' ,Address AS N'آدرس'"
+ "FROM Table1 WHERE Code = " + txtCode.Text.Trim() + "";
sda.SelectCommand.Connection = con;
dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
txtCode.ResetText();
}
else
{
MessageBox.Show("فردی با این مشخصات وجود ندارد");
}
}
}
finally
{
if (sda != null)
sda.Dispose();
if (dt != null)
dt.Dispose();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1_Load(null, null);
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}