今天正式上手c#的项目,不出所料,综合性很强,但是能学到很多东西,而且我发现果然有问题上google才是正确的选择,还好我英语比较好~~~还有42天了,加油啊!!!下面贴出今天的代码一边以后复习
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace _01Review
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//新建一个列表来存放数据库数据
List<ManagerInfo> list = new List<ManagerInfo>();
//从数据库表ManagerInfo中查询数据,将显示到DataGridView上
string connStr = @"data source = C:\Users\Administrator\Desktop\ItcastCater.db;version=3;"; //记得那个data source啊,刚开始我以后路径不能包含中文,最后google才发现问题的所在。。。
using (SQLiteConnection conn = new SQLiteConnection(connStr))//标准的引用SQLite的语句,不过我还是要补一下SQL Server的内容才行
{
//创建Command对象
SQLiteCommand cmd = new SQLiteCommand("select * from ManagerInfo", conn);
//打开链接
conn.Open();
//执行命令
SQLiteDataReader reader = cmd.ExecuteReader();
//读取
if (reader.HasRows)
{
while (reader.Read())
{
list.Add(new ManagerInfo()
{
Mid=Convert.ToInt32(reader["mid"]),
Mname=reader["mname"].ToString(),
Mpwd=reader["mpwd"].ToString(),
Mtype=Convert.ToInt32(reader["mtype"])
}
);
}
}
reader.Close();
dataGridView1.DataSource = list;
}
}
}
}