Материал из .Net Framework эксперт
Execute NonQuery Example: update
using System;
using System.Data; // Use ADO.NET namespace
using System.Data.SqlClient; // Use SQL Server data provider namespace
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
SqlConnection thisConnection = new SqlConnection(
@"Server=(local)\sqlexpress;Integrated Security=True;" +
"Database=northwind");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.rumandText = "UPDATE Products SET " +
"UnitPrice=UnitPrice*1.05 WHERE SupplierId=12";
int rowsAffected = thisCommand.ExecuteNonQuery();
Console.WriteLine("Rows Updated = {0}", rowsAffected);
thisConnection.Close();
}
}
Inserts (create) a new table
using System;
using System.Data.SqlClient;
public class UpdateRecord
{
public static void Main()
{
string connectionString = "Data Source=localhost;" +
"Initial Catalog=Northwind;Integrated Security=SSPI";
string SQL = "CREATE TABLE Users (UserName nvarchar(20), Password nvarchar(20) )";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(SQL, con);
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine(rowsAffected);
}
}
Updating Data Using Sql Statements
using System;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
MyConnection.Open();
String MyString = "UPDATE Employee SET FirstName = "Lee"";
SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);
MyCmd.ExecuteScalar();
MyConnection.Close();
}
}