c#.netDataGrid绑定数据库数据

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
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 DatabaseStudy
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void ButtonGetData_Click(object sender, RoutedEventArgs e)
        {
            string StrConnect = "server = localhost;database = Test;uid = Test;pwd = Nothjkkjjhing/123";
            string StrSelect = "select * from [Test].[dbo].[Info]";
            SqlConnection conn = new SqlConnection(StrConnect);
            //conn.Open();
            SqlCommand cmd = new SqlCommand(StrSelect, conn);
            SqlDataAdapter ShlyAdapter = new SqlDataAdapter(cmd);
            DataSet ShlyDataSet = new DataSet();
            ShlyAdapter.Fill(ShlyDataSet);
            DataGridShowData.ItemsSource = ShlyDataSet.Tables[0].DefaultView;
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注