using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
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.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace LoginWindow
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnDaoRu_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
if (ofd.ShowDialog()!=true)
{
return;
}
String filename = ofd.FileName;
// File.ReadLines一次性读取到内存,放到IEnumerable集合里面
//解决乱码问题Encoding.Default,用系统默认的编码
IEnumerable<String> lines= File.ReadLines(filename,Encoding.Default);
foreach (String line in lines)
{
//按竖线分割
String[] li=line.Split('|');
String name=li[0];
String age=li[1];
using (SqlConnection conn = new SqlConnection(DBHelper.connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_Customer(Name,age)values(@Name,@age)";
SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", name), new SqlParameter("@age", Convert.ToInt32(age)) };
cmd.Parameters.AddRange(paras);
cmd.ExecuteScalar();
}
}
}
MessageBox.Show("dsfd");
}
}
}
2.省市县联动查询案例
class DbHelper
{
public static SqlConnection conn = new SqlConnection("server=.;database=China;uid=sa;pwd=sa");
}
-----------
class Area
{
public int AreaId { get; set; }
public String AreaName { get; set; }
}
------------
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
using(da=new SqlDataAdapter("select * from Province",DbHelper.conn)){
ds= new DataSet();
da.Fill(ds,"Province");
//绑定listbox
List<Area> listPro=new List<Area>();
foreach (DataRow datarow in ds.Tables["Province"].Rows)
{
Area area = new Area();
area.AreaId = (int)datarow["Id"];
area.AreaName = (String)datarow["Name"];
listPro.Add(area);
}
lbxProvince.ItemsSource = listPro;
}
}
-------------
Grid-ListBox:
DisplayMemberPath="AreaName"
----------------
private void lbxProvince_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//获得选中的省对象
Area pro=(Area)lbxProvince.SelectedItem;
}
3.dataGridView
SqlDataAdapter da;
SqlDataReader dr;
DataSet ds;
SqlCommand cmd;
private void btnOpen_Click(object sender, EventArgs e)
{
String sql = "select * from books";
da = new SqlDataAdapter(sql, DbHelper.conn);
ds = new DataSet();
da.Fill(ds, "books");
//数据绑定
dgvShow.DataSource = ds.Tables[0];
}
//选中dgv,在txt中显示
private void dgvShow_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.txtId.Text = this.dgvShow.Rows[e.RowIndex].Cells["ID"].Value.ToString();
this.txtType.Text = this.dgvShow.Rows[e.RowIndex].Cells["Type"].Value.ToString();
this.txtName.Text = this.dgvShow.Rows[e.RowIndex].Cells["Name"].Value.ToString();
this.txtAuthor.Text = this.dgvShow.Rows[e.RowIndex].Cells["Author"].Value.ToString();
this.txtPrice.Text = this.dgvShow.Rows[e.RowIndex].Cells["Price"].Value.ToString();
this.txtCover.Text = this.dgvShow.Rows[e.RowIndex].Cells["Cover"].Value.ToString();
this.txtTotal.Text = this.dgvShow.Rows[e.RowIndex].Cells["TotalNum"].Value.ToString();
this.txtSale.Text = this.dgvShow.Rows[e.RowIndex].Cells["SaleNum"].Value.ToString();
}