下面的程序可使数据库的数据脱离数据库,保留在客户端来操作
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1_dataset_离线数据集
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["dbConnstr"].ConnectionString;
MessageBox.Show(connStr);
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(@"Data Source=.; Initial Catalog=master;User Id=sa;Password=******"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select Id from [Table] where Name=@Name";
cmd.Parameters.Add(new SqlParameter("@Name", "元芳"));
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.Fill(dataset);
}
}
DataTable table = dataset.Tables[0];
DataRowCollection rows = table.Rows;
for (int i = 0; i < rows.Count; i++)
{
DataRow row = rows[i];
int id = (int)row["Id"];
string name = (string)row["Name"];
MessageBox.Show(id+","+name);
}
}
}
}