Console.WriteLine("请输入用户名");
string username = Console.ReadLine();
Console.WriteLine("请输入密码");
string password = Console.ReadLine();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=F:\project\数据库\数据库\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select count(*) from T_users where UserName=@UserName and PassWord=@PassWord";
cmd.Parameters.Add(new SqlParameter("@UserName", username));
cmd.Parameters.Add(new SqlParameter("@PassWord", password));
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
Console.WriteLine("登录成功");
}
else
{
Console.WriteLine("登录失败");
}
}
}
参数化查询 防止sql注入
—————————————————————————————————————————————————————————————————————————————
namespace 导入导出
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (ofdimport.ShowDialog() != DialogResult.OK) 查看ofdimport控件的状态 如果没选择文件 就返回
{
return;
}
using (FileStream filestream = File.OpenRead(ofdimport.FileName)) 创建FileStream 类的对象来存放 ofdimport中的文件
{
using (StreamReader sr = new StreamReader(filestream)) 创建 StreamReader 的对象来读取filestream 中的文件
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDBFilename=F:\project\导入导出\导入导出\Info.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_person (Name,Age) values(@Name,@Age)";
string line = null;
while ((line = sr.ReadLine()) != null) 逐行读取
{
string[] str = line.Split('|'); 用"|" 分割每一行
string name = str[0];
int age = Convert.ToInt32(str[1]);
cmd.Parameters.Clear(); 清除以前的参数 必须清除 否则会报错
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery();
}
}
}
}
}
MessageBox.Show("导入成功");
}
}
}
写这个程序用到了io 没学过 在视频里找也没有 后来发现这好像是黑马课程的一部分 没办法 只好现学现卖了
—————————————————————————————————————————————————————————————————————————————
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
namespace 归属地查询
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "delete from T_number";
cmd.ExecuteNonQuery();
}
}
string path = dlg.SelectedPath;
string[] filepaths = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
foreach(string filepath in filepaths)
{
string 运营商名称 =Path.GetFileNameWithoutExtension(filepath);
string[] lines = File.ReadAllLines(filepath, Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split('-');
string startNo=strs[0];
string endNo=strs[1];
string cityname = strs[2];
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_number (StartNo,EndNo,Name) values (@StartNo,@EndNo,@Name)";
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("StartNo", startNo));
cmd.Parameters.Add(new SqlParameter("EndNo", endNo));
cmd.Parameters.Add(new SqlParameter("Name", cityname));
cmd.ExecuteNonQuery();
}
}
}
}
}
private void select_Click(object sender, EventArgs e)
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_number where StartNo<=@No and EndNo>=@No";
cmd.Parameters.Add(new SqlParameter("No",txtMobileNo.Text.ToString()));
using (SqlDataReader sdr = cmd.ExecuteReader())
{
try
{
string 运营商名字 = sdr.GetString(sdr.GetOrdinal("Name"));
MessageBox.Show(运营商名字);
}
catch
{
MessageBox.Show("查询失败,没有找到此条记录");
}
}
}
}
}
}
}
用SelectedPath属性得到当前选择的路径
用Directory类的GetFiles方法来得到当前路径下的所有文件
GetFiles方法可以有三个参数 1.要取的文件所在的文件夹的路径 2.要取得的文件的格式
3.SearchOption 枚举类型 可以选择是否读取子文件夹下的文件
用ReadAllLines 读取文件 因为文件较小 如果文件较大 可以用StreamReader
解决读出的数据乱码问题
File.ReadAllLines(filepath, Encoding.Default) 第二个参数可以指定编码类型 选择与要导入的文件相同的编码类型