Skip to content

Commit 21c3e18

Browse files
Add samples (#821)
Related to the second part of porting documents from .NET Framework documents.
1 parent ac8bb3f commit 21c3e18

8 files changed

+451
-2
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Data;
3+
using Microsoft.Data.SqlClient;
4+
5+
namespace SqlCommandBuilderCS
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
string cnnst = "Data Source=(local);Initial Catalog=Northwind;"
12+
+ "Integrated Security=SSPI";
13+
string queryst = "SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers";
14+
string newQueryString = "SELECT CustomerID, City, Region FROM Customers";
15+
string tablen = "Customers";
16+
DataSet ds = SelectSqlRows(cnnst, queryst, newQueryString, tablen);
17+
}
18+
19+
public static DataSet SelectSqlRows(string connectionString,
20+
string queryString, string newQueryString, string tableName)
21+
{
22+
using (SqlConnection connection = new SqlConnection(connectionString))
23+
{
24+
// <Snippet1>
25+
// Assumes that connection is a valid SqlConnection object
26+
// inside of a using block.
27+
SqlDataAdapter adapter = new SqlDataAdapter();
28+
adapter.SelectCommand = new SqlCommand(queryString, connection);
29+
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
30+
builder.QuotePrefix = "[";
31+
builder.QuoteSuffix = "]";
32+
// </Snippet1>
33+
34+
// <Snippet2>
35+
// Generate the update command automatically by SqlCommandBuilder
36+
Console.WriteLine(builder.GetUpdateCommand().CommandText);
37+
// </Snippet2>
38+
39+
connection.Open();
40+
41+
DataSet dataSet = new DataSet();
42+
adapter.Fill(dataSet, tableName);
43+
44+
// <Snippet3>
45+
// Assumes an open SqlConnection and SqlDataAdapter inside of a using block.
46+
adapter.SelectCommand.CommandText = newQueryString;
47+
builder.RefreshSchema();
48+
49+
dataSet.Tables.Remove(dataSet.Tables[tableName]);
50+
adapter.Fill(dataSet, tableName);
51+
// </Snippet3>
52+
53+
//code to modify data in DataSet here
54+
builder.GetUpdateCommand();
55+
56+
//Without the SqlCommandBuilder this line would fail
57+
adapter.Update(dataSet, tableName);
58+
59+
return dataSet;
60+
}
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Data;
3+
using Microsoft.Data.SqlClient;
4+
5+
namespace SqlCommandCS
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
string str = "Data Source=(local);Initial Catalog=Northwind;"
12+
+ "Integrated Security=SSPI";
13+
14+
CreateStoredProcedure(str);
15+
CreateCommand(str);
16+
}
17+
18+
private static void CreateStoredProcedure(string connectionString)
19+
{
20+
using (SqlConnection connection = new SqlConnection(connectionString))
21+
{
22+
// <Snippet3>
23+
// Assumes connection is a valid SqlConnection.
24+
string queryString = "CREATE PROCEDURE InsertCategory " +
25+
"@CategoryName nchar(15), " +
26+
"@Identity int OUT " +
27+
"AS " +
28+
"INSERT INTO Categories (CategoryName) VALUES(@CategoryName) " +
29+
"SET @Identity = @@Identity " +
30+
"RETURN @@ROWCOUNT";
31+
32+
SqlCommand command = new SqlCommand(queryString, connection);
33+
command.ExecuteNonQuery();
34+
// </Snippet3>
35+
}
36+
}
37+
38+
private static void CreateCommand(string connectionString)
39+
{
40+
using (SqlConnection connection = new SqlConnection(connectionString))
41+
{
42+
SqlCommand command = new SqlCommand(connection);
43+
44+
// <Snippet1>
45+
// Assumes connection is a valid SqlConnection.
46+
connection.Open();
47+
48+
string queryString = "INSERT INTO Customers " +
49+
"(CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
50+
51+
SqlCommand command = new SqlCommand(queryString, connection);
52+
Int32 recordsAffected = command.ExecuteNonQuery();
53+
// </Snippet1>
54+
55+
// <Snippet2>
56+
// Assumes command is a valid SqlCommand with an open connection.
57+
command.CommandText = "InsertCategory";
58+
command.CommandType = CommandType.StoredProcedure;
59+
60+
SqlParameter parameter = command.Parameters.Add("@RowCount", SqlDbType.Int);
61+
parameter.Direction = ParameterDirection.ReturnValue;
62+
63+
parameter = command.Parameters.Add("@CategoryName", SqlDbType.NChar, 15);
64+
65+
parameter = command.Parameters.Add("@Identity", SqlDbType.Int);
66+
parameter.Direction = ParameterDirection.Output;
67+
68+
command.Parameters["@CategoryName"].Value = "New Category";
69+
command.ExecuteNonQuery();
70+
71+
Int32 categoryID = (Int32) command.Parameters["@Identity"].Value;
72+
Int32 rowCount = (Int32) command.Parameters["@RowCount"].Value;
73+
// </Snippet2>
74+
}
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Data;
3+
using Microsoft.Data.SqlClient;
4+
5+
class Class1
6+
{
7+
static void Main()
8+
{
9+
int ret = AddProductCategory("newval", GetConnectionString());
10+
Console.WriteLine(ret.ToString());
11+
Console.ReadLine();
12+
}
13+
14+
// <Snippet1>
15+
static public int AddProductCategory(string newName, string connString)
16+
{
17+
Int32 newProdID = 0;
18+
string sql =
19+
"INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
20+
+ "SELECT CAST(scope_identity() AS int)";
21+
using (SqlConnection conn = new SqlConnection(connString))
22+
{
23+
SqlCommand cmd = new SqlCommand(sql, conn);
24+
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
25+
cmd.Parameters["@name"].Value = newName;
26+
try
27+
{
28+
conn.Open();
29+
newProdID = (Int32)cmd.ExecuteScalar();
30+
}
31+
catch (Exception ex)
32+
{
33+
Console.WriteLine(ex.Message);
34+
}
35+
}
36+
return (int)newProdID;
37+
}
38+
39+
// </Snippet1>
40+
static private string GetConnectionString()
41+
{
42+
// To avoid storing the connection string in your code,
43+
// you can retrieve it from a configuration file.
44+
return "Data Source=(local);Initial Catalog=AdventureWorks;"
45+
+ "Integrated Security=true";
46+
}
47+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using Microsoft.Data.SqlClient;
3+
using System.Data;
4+
5+
namespace NextResultCS
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
string s = GetConnectionString();
12+
SqlConnection c = new SqlConnection(s);
13+
GetCustomers(c);
14+
PrintCustomersOrders(c, c);
15+
Console.ReadLine();
16+
}
17+
18+
static DataSet GetCustomers(SqlConnection connection)
19+
{
20+
using (connection)
21+
{
22+
// <Snippet1>
23+
// Assumes that connection is a valid SqlConnection object.
24+
string queryString =
25+
"SELECT CustomerID, CompanyName FROM dbo.Customers";
26+
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
27+
28+
DataSet customers = new DataSet();
29+
adapter.Fill(customers, "Customers");
30+
// </Snippet1>
31+
return customers;
32+
}
33+
}
34+
35+
static void PrintCustomersOrders(SqlConnection customerConnection, SqlConnection orderConnection)
36+
{
37+
using (customerConnection)
38+
using (orderConnection)
39+
{
40+
// <Snippet2>
41+
// Assumes that customerConnection and orderConnection are valid SqlConnection objects.
42+
SqlDataAdapter custAdapter = new SqlDataAdapter(
43+
"SELECT * FROM dbo.Customers", customerConnection);
44+
SqlDataAdapter ordAdapter = new SqlDataAdapter(
45+
"SELECT * FROM Orders", orderConnection);
46+
47+
DataSet customerOrders = new DataSet();
48+
49+
custAdapter.Fill(customerOrders, "Customers");
50+
ordAdapter.Fill(customerOrders, "Orders");
51+
52+
DataRelation relation = customerOrders.Relations.Add("CustOrders",
53+
customerOrders.Tables["Customers"].Columns["CustomerID"],
54+
customerOrders.Tables["Orders"].Columns["CustomerID"]);
55+
56+
foreach (DataRow pRow in customerOrders.Tables["Customers"].Rows)
57+
{
58+
Console.WriteLine(pRow["CustomerID"]);
59+
foreach (DataRow cRow in pRow.GetChildRows(relation))
60+
Console.WriteLine("\t" + cRow["OrderID"]);
61+
}
62+
// </Snippet2>
63+
}
64+
}
65+
66+
static private string GetConnectionString()
67+
{
68+
// To avoid storing the connection string in your code,
69+
// you can retrieve it from a configuration file.
70+
return "Data Source=(local);Initial Catalog=Northwind;"
71+
+ "Integrated Security=SSPI";
72+
}
73+
}
74+
}

doc/samples/SqlDataAdapter_SqlDataAdapter.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
22
using System.Data;
3-
//<Snippet1>
43
using Microsoft.Data.SqlClient;
54

65
class Program
76
{
87
static void Main()
98
{
109
}
10+
//<Snippet1>
1111
public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
1212
{
13+
// Assumes that connection is a valid SqlConnection object
1314
SqlDataAdapter adapter = new SqlDataAdapter();
1415
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
1516

@@ -45,5 +46,19 @@ public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
4546

4647
return adapter;
4748
}
49+
//</Snippet1>
50+
51+
public static SqlDataAdapter CustomerUpdateCommand(SqlDataAdapter adapter)
52+
{
53+
//<Snippet2>
54+
// Assumes that connection is a valid SqlAdapter object
55+
adapter.UpdateCommand.Parameters.Add("@CompanyName",
56+
SqlDbType.VarChar, 15, "CompanyName");
57+
SqlParameter parameter = adapter.UpdateCommand.Parameters.Add("@CustomerID",
58+
SqlDbType.Char, 5, "CustomerID");
59+
parameter.SourceVersion = DataRowVersion.Original;
60+
//</Snippet2>
61+
return adapter;
62+
}
63+
4864
}
49-
//</Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Microsoft.Data.SqlClient;
3+
using System.Data;
4+
5+
namespace NextResultCS
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
string s = GetConnectionString();
12+
SqlConnection c = new SqlConnection(s);
13+
GetSchemaInfo(c);
14+
Console.ReadLine();
15+
}
16+
// <Snippet1>
17+
static void GetSchemaInfo(SqlConnection connection)
18+
{
19+
using (connection)
20+
{
21+
SqlCommand command = new SqlCommand(
22+
"SELECT CategoryID, CategoryName FROM Categories;",
23+
connection);
24+
connection.Open();
25+
26+
SqlDataReader reader = command.ExecuteReader();
27+
28+
// Retrieve schema information about the current result-set.
29+
DataTable schemaTable = reader.GetSchemaTable();
30+
31+
foreach (DataRow row in schemaTable.Rows)
32+
{
33+
foreach (DataColumn column in schemaTable.Columns)
34+
{
35+
Console.WriteLine(String.Format("{0} = {1}",
36+
column.ColumnName, row[column]));
37+
}
38+
}
39+
40+
// Always call the Close method when you have finished using the DataReader object.
41+
reader.Close();
42+
}
43+
}
44+
// </Snippet1>
45+
46+
static private string GetConnectionString()
47+
{
48+
// To avoid storing the connection string in your code,
49+
// you can retrieve it from a configuration file.
50+
return "Data Source=(local);Initial Catalog=Northwind;"
51+
+ "Integrated Security=SSPI";
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)