C#_数据库基本交互

//app.config

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <startup> 

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

  <connectionStrings>

    <add name="dbConnStr" connectionString="Data Source=服务器地址;Initial Catalog=数据库;User ID=账号(可以是sa);Password=密码"/>

  </connectionStrings>

</configuration>


//content

 

 

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 DataBaseTest

{

    /// <summary>

    /// MainWindow.xaml 的交互逻辑

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }



        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            using (SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=xxx;User ID=sa;Password=xxx"))

            {

                conn.Open();

                using(SqlCommand cmm = conn.CreateCommand())

                {

                    //cmm.CommandText = "select * from T_Student";

                   //没有返回值

                   //cmm.ExecuteNonQuery();

                   //有一个返回值

                   //insert into T_Student(Name,Age) output inserted.Id values ('aaa',123);

                   // object o = cmm.ExecuteScalar();

                   //多个返回值

                    //using (SqlDataReader reader = cmm.ExecuteReader())

                    //{

                    //    while(reader.Read())

                    //    {

                    //        MessageBox.Show(reader.GetString(1));

                    //    }

                    //}

                    cmm.CommandText = "select * from T_Student where Name=@Name";

                    cmm.Parameters.Add(new SqlParameter("@Name", txtSearch.Text));

                    //查询输入内容

                    using (SqlDataReader reader = cmm.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            MessageBox.Show(reader.GetString(1));

                        }

                    }



                    

                }

            }

            MessageBox.Show("successful");

        }



        private void 离线数据集_Click(object sender, RoutedEventArgs e)

        {

            using (SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=xxxx;User ID=sa;Password=xxx"))

            {

                conn.Open();

                using (SqlCommand cmm = conn.CreateCommand())

                {

                    cmm.CommandText = "select * from T_Student where Name=@Name";

                    cmm.Parameters.Add(new SqlParameter("@Name", txtSearch.Text));



                    //SqlDataAdapter是一个把查询结果填充到DataSet中

                    SqlDataAdapter adapt = new SqlDataAdapter(cmm);

                    //本地集合

                    DataSet dataset = new DataSet();

                    adapt.Fill(dataset);



                    DataTable table = dataset.Tables[0];



                    DataRowCollection rows = table.Rows;



                    for (int i = 0; i < rows.Count; i++)

                    {

                        DataRow row = rows[i];

                        int age = (int)row["Age"];

                        string name = (string)row["Name"];

                        MessageBox.Show("name: "+name+" age: "+age);



                    }

                }

            }

        }



        private void btnConStr_Click(object sender, RoutedEventArgs e)

        {

            //需要添加system.Configration reference

            string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;

            MessageBox.Show(connStr);

            //SqlHelper.ExecuteNonQuery("insert into T_Student(Name,Age) values ('eee',123)");

            MessageBox.Show("succeed insert");



            //DataSet dataset = new DataSet();

            //using (SqlConnection conn = new SqlConnection(connStr))

            //{

            //    conn.Open();

            //    using (SqlCommand cmm = conn.CreateCommand())

            //    {

            //        cmm.CommandText = "select * from T_Student where Name=@Name";

            //        cmm.Parameters.Add(new SqlParameter("@Name", txtSearch.Text));



            //        //SqlDataAdapter是一个把查询结果填充到DataSet中

            //        SqlDataAdapter adapt = new SqlDataAdapter(cmm);

            //        //本地集合

                    

            //        adapt.Fill(dataset);



            //        DataTable table = dataset.Tables[0];



            //        DataRowCollection rows = table.Rows;



            //        for (int i = 0; i < rows.Count; i++)

            //        {

            //            DataRow row = rows[i];

            //            int age = (int)row["Age"];

            //            string name = (string)row["Name"];

            //            MessageBox.Show("name: " + name + " age: " + age);



            //        }

            //    }

            //}

            

        }



        private void Button_Click_2(object sender, RoutedEventArgs e)

        {

            //DataSet ds = SqlHelper.ExecuteDataSet("select * from T_Student");

            //foreach (DataRow row in ds.Tables[0].Rows)

            //{

            //    string name = (string)row["Name"];

            //    MessageBox.Show(name);

            //}

            DataTable ds = SqlHelper.ExecuteDataTable("select * from T_Student");

            foreach (DataRow row in ds.Rows)

            {

                string name = (string)row["Name"];

                MessageBox.Show(name);

            }

        }

    }

}


 

 

你可能感兴趣的:(数据库)