博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#_数据库基本交互
阅读量:6306 次
发布时间:2019-06-22

本文共 5597 字,大约阅读时间需要 18 分钟。

//app.config

 

//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{    ///     /// MainWindow.xaml 的交互逻辑    ///     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);            }        }    }}

 

 

转载于:https://www.cnblogs.com/MarchThree/p/3720440.html

你可能感兴趣的文章
十四、转到 linux
查看>>
Got error 241 'Invalid schema
查看>>
ReferenceError: event is not defined
查看>>
男人要内在美,更要外在美
查看>>
为什么要跟别人比?
查看>>
app启动白屏
查看>>
Oracle 提高查询性能(基础)
查看>>
学习知识应该像织网一样去学习——“网状学习法”
查看>>
Hadoop集群完全分布式安装
查看>>
QString,char,string之间赋值
查看>>
我的友情链接
查看>>
Nginx+mysql+php-fpm负载均衡配置实例
查看>>
shell脚本操作mysql数据库 (部份参考)
查看>>
MySql之基于ssl安全连接的主从复制
查看>>
informix的逻辑日志和物理日志分析
查看>>
VMware.Workstation Linux与windows实现文件夹共享
查看>>
ARM inlinehook小结
查看>>
wordpress admin https + nginx反向代理配置
查看>>
管理/var/spool/clientmqueue/下的大文件
查看>>
HTML学习笔记1—HTML基础
查看>>