БД 12 лаба (ADO)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Configuration;
namespace ADO
{
class Program
{
static void Main(string[] args)
{
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
cn.Open();
string strSQL = "DELETE FROM table1";
SqlCommand myCommand = new SqlCommand(strSQL, cn);
myCommand.ExecuteNonQuery();
strSQL = "INSERT table1 (field1, field2) VALUES (@a, @b)";
myCommand = new SqlCommand(strSQL, cn);
SqlParameter param = new SqlParameter();
param.ParameterName = "@a";
param.Value = 1;
param.SqlDbType = SqlDbType.Int;
myCommand.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "@b";
param.Value = "dgsd";
param.SqlDbType = SqlDbType.NVarChar;
param.Size = 30;
myCommand.Parameters.Add(param);
myCommand.ExecuteNonQuery();
strSQL = "UPDATE table1 SET field1 += @a";
myCommand = new SqlCommand(strSQL, cn);
param = new SqlParameter();
param.ParameterName = "@a";
param.Value = 5;
param.SqlDbType = SqlDbType.Int;
myCommand.Parameters.Add(param);
myCommand.ExecuteNonQuery();
strSQL = "Select * From table1";
myCommand = new SqlCommand(strSQL, cn);
SqlDataReader dataReader;
dataReader = myCommand.ExecuteReader();
while (dataReader.Read())
{
for (int i=0;i<2;i++)
Console.Write("{0}\t", dataReader.GetValue(i).ToString());
Console.WriteLine();
}
dataReader.Close();
SqlDataAdapter datable1 = new SqlDataAdapter();
strSQL = "Select * From table1";
SqlCommand selectcommand = new SqlCommand(strSQL, cn);
datable1.SelectCommand = selectcommand;
strSQL = "INSERT table1 (field1, field2) VALUES (@a, @b)";
SqlCommand insertcommand = new SqlCommand(strSQL, cn);
SqlParameterCollection pc = insertcommand.Parameters;
pc.Add("@a", SqlDbType.Int, 0, "field1");
pc.Add("@b", SqlDbType.NVarChar, 0, "field2");
datable1.InsertCommand = insertcommand;
strSQL = "UPDATE table1 SET field1 = @new_a, field2 = @new_b WHERE field1 = @old_a AND field2 = @old_b";
SqlCommand updatecommand = new SqlCommand(strSQL, cn);
pc = updatecommand.Parameters;
pc.Add("@new_a", SqlDbType.Int, 0, "field1");
pc.Add("@new_b", SqlDbType.NVarChar, 0, "field2");
param = pc.Add("@old_a", SqlDbType.Int, 0, "field1");
param.SourceVersion = DataRowVersion.Original;
param = pc.Add("@old_b", SqlDbType.NVarChar, 0, "field2");
param.SourceVersion = DataRowVersion.Original;
datable1.UpdateCommand = updatecommand;
strSQL = "DELETE FROM table1 WHERE field1 = @a";
SqlCommand deletecommand = new SqlCommand(strSQL, cn);
pc = deletecommand.Parameters;
pc.Add("@a", SqlDbType.Int, 0, "field1");
pc.Add("@b", SqlDbType.NVarChar, 0, "field2");
datable1.DeleteCommand = deletecommand;
DataTable temp = new DataTable("table1");
datable1.Fill(temp);
DataRow row = temp.NewRow();
row["field1"] = 3;
row["field2"] = "sdfg";
temp.Rows.Add(row);
DataRow row2 = temp.NewRow();
row2["field1"] = 5;
row2["field2"] = "sdfg";
temp.Rows.Add(row2);
temp.Rows[0]["field1"]= 5;
temp.Rows[0]["field2"] = "asfas";
temp.Rows[1].Delete();
datable1.Update(temp);
DataTableReader dtReader = temp.CreateDataReader();
while (dtReader.Read())
{
for (int i = 0; i < dtReader.FieldCount; i++)
Console.Write("{0}\t", dtReader.GetValue(i).ToString());
Console.WriteLine();
}
dtReader.Close();
Console.WriteLine("УРА!");
Console.ReadKey();
cn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Облом!");
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}