46 строки
1.2 KiB
C#
46 строки
1.2 KiB
C#
// <Snippet1>
|
|
using System;
|
|
using System.Data;
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace SqlCommandBuilderCS
|
|
{
|
|
class Program
|
|
{
|
|
static void Main()
|
|
{
|
|
string cnnst = "";
|
|
string queryst = "";
|
|
string tablen = "";
|
|
DataSet ds = SelectSqlRows(cnnst, queryst, tablen);
|
|
|
|
}
|
|
|
|
public static DataSet SelectSqlRows(string connectionString,
|
|
string queryString, string tableName)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
|
{
|
|
SqlDataAdapter adapter = new SqlDataAdapter();
|
|
adapter.SelectCommand = new SqlCommand(queryString, connection);
|
|
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
|
|
|
|
connection.Open();
|
|
|
|
DataSet dataSet = new DataSet();
|
|
adapter.Fill(dataSet, tableName);
|
|
|
|
//code to modify data in DataSet here
|
|
|
|
builder.GetUpdateCommand();
|
|
|
|
//Without the SqlCommandBuilder this line would fail
|
|
adapter.Update(dataSet, tableName);
|
|
|
|
return dataSet;
|
|
}
|
|
}
|
|
// </Snippet1>
|
|
}
|
|
}
|