<< >>


4.3.3.

Command . , - . , , - ? , , , . , , , . , , . , ExecuteNonQuery commandText :

myCommand.CommandText =
"UPDATE SET = '' WHERE = 3";

, , , . :

myCommand.CommandText = " UPDATE SET
= '-_,___'
WHERE = '-_,___' ";

, SQL, . :

myCommand.CommandText =
"UPDATE SET = @Family WHERE = @TouristID";

@Family - , @TouristID - . , .

, .

 

ExecuteNonQuery

 

Windows- . (. 87).

. 87. ,

:

using System.Data.SqlClient;

conn:

SqlConnection conn = null;

btnUpdate :

private void btnUpdate_Click(object sender, System.EventArgs e) {
try {
string Family = Convert.ToString(this.txtFamilyUpdate.Text);
int TouristID = int.Parse(this.txtTouristIDUpdate.Text);
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
@"D:\BM\For ADO\BDTur_firmSQL2.mdf" +
"Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
SqlCommand myCommand = conn.CreateCommand();
myCommand.CommandText = "UPDATE SET = @Family WHERE [ ] = @TouristID";
myCommand.Parameters.Add("@Family", SqlDbType.NVarChar, 50);
myCommand. Parameters["@Family"].Value = Family;
myCommand.Parameters.Add("@TouristID", SqlDbType.Int, 4);
myCommand. Parameters["@TouristID"].Value = TouristID;
int UspeshnoeIzmenenie = myCommand.ExecuteNonQuery();
if (UspeshnoeIzmenenie !=0) {
MessageBox.Show(" ", " ");
}
else {
MessageBox.Show( ", " ");
}
conn.Close();
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}

, finally , , . , txtFamilyUpdate txtTouristIDUpdate, Family TouristID. - @Family @TouristID. Command Add Parameters, Family TouristID. Add . . Add 16.

Add, Parameters Command OLE DB .

16. Add

parameterName

sqlDbType

size

sourceColumn

DataSet,

btnInsert.

private void btnInsert_Click(object sender, System.EventArgs e) {
try {
int TouristID = int.Parse(this.txtTouristIDInsert.Text);
string Family = Convert.ToString(this.txtFamilyInsert.Text);
string FirstName = Convert.ToString(this.txtFirstNameInsert.Text);
string MiddleName = Convert.ToString(this.txtMiddleNameInsert.Text);
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
@"D:\\For ADO\BDTur_firmSQL2.mdf" +
";Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
SqlCommand myCommand = conn.CreateCommand();
myCommand.CommandText = "INSERT INTO " +
" ([ ], , , ) " +
"VALUES (@TouristID, @Family, @FirstName, @MiddleName)";
myCommand.Parameters.Add("@TouristID", SqlDbType.Int, 4);
myCommand. Parameters["@TouristID"].Value = TouristID;
myCommand.Parameters.Add("@Family", SqlDbType.NVarChar, 50);
myCommand. Parameters["@Family"].Value = Family;
myCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50);
myCommand. Parameters["@FirstName"].Value = FirstName;
myCommand.Parameters.Add("@MiddleName", SqlDbType.NVarChar, 50);
myCommand. Parameters["@MiddleName"].Value = MiddleName;
int UspeshnoeIzmenenie = myCommand.ExecuteNonQuery();
if (UspeshnoeIzmenenie !=0) {
MessageBox.Show(" ", " ");
}
else {
MessageBox.Show( ", " ");
}
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
finally {
conn.Close();
}
}

: @TouristID, @Family, @FirstName, @MiddleName. .

btnDelete.

private void btnDelete_Click(object sender, System.EventArgs e) {
try {
int TouristID = int.Parse(this.txtTouristIDDelete.Text);
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
@"D:\\For ADO\BDTur_firmSQL2.mdf" +
"integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
SqlCommand myCommand = conn.CreateCommand();
myCommand.CommandText = "DELETE FROM " +
"WHERE [ ] = @TouristID";
myCommand.Parameters.Add("@TouristID", SqlDbType.Int, 4);
myCommand. Parameters["@TouristID"].Value = TouristID;
int UspeshnoeIzmenenie = myCommand.ExecuteNonQuery();
if (UspeshnoeIzmenenie !=0) {
MessageBox.Show(" ", " ");
}
else {
MessageBox.Show( ", " ");
}
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
finally {
conn.Close();
}
}

. , . , Management Studio (. 88 - 90).

. 88.

. 89.

. 90.

 

ExecuteScalar

 

ExecuteScalar Command Windows- - ( , ) .

 

ExecuteReader

 

ExecuteReader. - .

. ListBox, Dock Bottom. Splitter, Dock Bottom. , ListView, Dock Fill.

, ListView: Properties Columns (...).

Column Header Collection Editor :

Name Text
chTouristID
chFamily
chFirstName
chMiddleName

View Details. - GridLines True. .

:

using System.Data.SqlClient;

conn dataReader:

SqlConnection conn = null;
SqlDataReader dataReader;

:

public Form1() {
InitializeComponent();
try {
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
@"D:\\For ADO\BDTur_firmSQL2.mdf" +
"integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
SqlCommand myCommand = conn.CreateCommand();
myCommand.CommandText = "SELECT * FROM ";
dataReader = myCommand.ExecuteReader();
while (dataReader.Read()) {
// , dataReader,
// Get
int TouristID = dataReader.GetInt32(0);
string Family = dataReader.GetString(1);
string FirstName = dataReader.GetString(2);
string MiddleName = dataReader.GetString(3);
// listBox1
listBox1.Items.Add(" : " + TouristID+ " : " + Family + " : "+ FirstName + " : " + MiddleName);
// item ListViewItem
// dataReader
ListViewItem item = new ListViewItem(new string[]
{Convert.ToString(dataReader[0]), Convert.ToString(dataReader[1]),
Convert.ToString(dataReader[2]), Convert.ToString(dataReader[3])});
listView1.Items.Add(item);
}
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
finally {
dataReader.Close();
conn.Close();
}
}

Get , DataReader, .

. ListBox ListView (. 91).

ListView , DataGridView. , DataSet, DataGridView :

dataGridView1.DataSource = dataset.Tables["_"].DefaultView;

:

dataGridView1.DataSource = dataset;

DataReader - :

dataGridView1.DataSource = datareader;

. 91. ExecuteReader

DataTable. DataTable , .

. DataGrid, Dock Fill.

:

using System.Data.SqlClient;

:

SqlConnection conn = null;
// FullDataTable,
DataTable FullDataTable = new DataTable();
// FullDataTable
DataTable ShemaDataTable = new DataTable();
SqlDataReader dataReader;
SqlCommand myCommand;
// objectRow
object[] objectRow;
// myDataRow
DataRow myDataRow;
:
public Form1() {
InitializeComponent();
try {
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
@"D:\\For ADO\BDTur_firmSQL2.mdf" +
"integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
myCommand = conn.CreateCommand();
myCommand.CommandText = "SELECT * FROM ";
dataReader = myCommand.ExecuteReader();
// GetSchemaTable,
// ShemaDataTable
ShemaDataTable = dataReader.GetSchemaTable();
// FieldCount .
// objectRow
objectRow = new object[dataReader.FieldCount];
// FullDataTable
for(int i =0; i <dataReader.FieldCount; i++) {
FullDataTable.Columns.Add(ShemaDataTable.Rows[i]["ColumnName"].ToString(),
((System.Type)ShemaDataTable.Rows[i]["DataType"]));
}
// FullDataTable
while(dataReader.Read()) {
dataReader. GetValues(objectRow);
myDataRow = FullDataTable.Rows.Add(objectRow);
}
//
dataGrid1 dataGrid1 .DataSource = FullDataTable;
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
finally {
dataReader.Close();
conn.Close();
}
}

(. 92):

. 92.

 



<< >>