WPF SqlClient
SQLサーバ−接続
接続方法は基本的には同じ。認証方法を考慮すること。
参考:http://jeanne.wankuma.com/tips/csharp/sqlserver/open.html
using System.Data.SqlClient;
using System.Data;namespace WpfApplication1
{
///
/// MainWindow.xaml の相互作用ロジック
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string dbSQL = "Server=w7-pc;Database=dbSQLHN;Trusted_Connection=SSPI;";
//string dbSQL = "Server=W7-PC;UID=sa;PWD=;DataBase=dbSQLHN"SqlConnection conSQL = new SqlConnection(dbSQL);
conSQL.Open();
string cmdSQL = "select * from table01 order by code";DataTable dtTBL = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter(cmdSQL,conSQL);
SqlCommandBuilder sqlCB = new SqlCommandBuilder(sqlDA);
sqlDA.Fill(dtTBL);
dataGrid1.ItemsSource = dtTBL.DefaultView;
}
}
}