2007年1月13日星期六

.net最后冲刺

今天是.net复习的最后一天了,感谢王老师,风月的全面帮助~~!!
特别是今天,看了王老师给的文章里有关DataAdpater控键,可是我无论如何在05里面找不出来,于是问风月,结果¥……※×!◎#¥%……※×()
结果。。。结果。。。演变成了代码流了。。。而且读出数据也成功了!!!
为了纪念这历史性的时刻,我把代码贴出来保存下-_-!
首先DBsqlconnection.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication3
{
class DBsqlconnection
{
private SqlConnection sqlcon = null;
private readonly string sqlconstr="server=localhost;uid=sa;pwd=;database=Book"; public DBsqlconnection()
{
this.sqlcon = new SqlConnection(this.sqlconstr);//数据库连接对象实例化
}
public SqlConnection getconnection()
{
return this.sqlcon;
}
}
}

然后DBsqldatareader.cs
using System;
using System.Data;
using System.Data.SqlClient;using System.Windows.Forms;
namespace WindowsApplication3
{
class DBsqldatareader
{
private DBsqlconnection DBsqlcon = new DBsqlconnection();
private SqlConnection sqlcon = null;
private SqlCommand sqlcom= null;
private readonly string readcom = "select * from BookInfo";
private SqlDataReader reader = null;
public DBsqldatareader()
{
this.sqlcon = this.DBsqlcon.getconnection();
this.sqlcom = new SqlCommand(this.readcom, this.sqlcon);
}//sqlcommand shili hua
public SqlDataReader getreader()
{
try
{
this.sqlcon.Open();
this.reader = this.sqlcom.ExecuteReader();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
return this.reader;
}
public void close()
{
this.reader.Close();
this.sqlcon.Close();
}
}
}
再是form1.cs
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
private DataColumn dc1 = new DataColumn("BookID", typeof(string));
private DataColumn dc2 = new DataColumn("BookType", typeof(string));
private DataColumn dc3 = new DataColumn("BookName",typeof(string));
private DataColumn dc4 = new DataColumn("BookPrice", typeof(string));
private DataColumn dc5 = new DataColumn("BookPic", typeof(string));
private DataColumn dc6 = new DataColumn("BookContent", typeof(string));
private DataRow dr = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ // TODO: 这行代码将数据加载到表“bookDataSet1.BookInfo”中。您可以根据需要移动或移除它。 //this.loaddata();
}
private void loaddata()
{
SqlDataReader reader = null;
DBsqldatareader DBreader=new DBsqldatareader();
reader = DBreader.getreader();
this.dt.Columns.AddRange(new DataColumn[]
{
this.dc1, this.dc2, this.dc3, this.dc4, this.dc5, this.dc6
});
this.ds.Tables.Add(this.dt);
this.dataGridView1.DataSource = this.ds;
this.dataGridView1.DataMember = this.dt.TableName;
for (; reader.Read(); )
{
this.dr = this.dt.NewRow();
this.dr[0] = reader[0].ToString();
this.dr[1] = reader[1].ToString();
this.dr[2] = reader[2].ToString();
this.dr[3] = reader[3].ToString();
this.dr[4] = reader[4].ToString();
this.dr[5] = reader[5].ToString();
this.dt.Rows.Add(this.dr);
}
reader.Close();
DBreader.close();
}
private void button1_Click(object sender, EventArgs e)
{
this.bookInfoTableAdapter.Fill(this.bookDataSet1.BookInfo);
}
private void button2_Click(object sender, EventArgs e)
{
this.bookInfoTableAdapter.Update(this.bookDataSet1);
}
}
}
好了,大致也是这样,他的意思就是自己先做张虚表然后把物理表映射到里面来。虽然当时也看的云里雾里的,不过还是很认真的在听。这样大概讲了两个小时左右。
然后,不太擅长鼠标流的他突然发现了....
然后....只用了2分钟....
然后※×()◎#¥%……※×()——
当然我能理解他的心情,对于他来说的确是很震惊,。。。
对于我来说流汗之余有点庆幸~~
我说你因该去搞.net开发了-_-!!!
一个字“强”~
嗯嗯,不知道其他同学复习的怎样了,大家好像都没什么信心呢。一连考了两次jsp连.net是什么都要忘记了。
不过照鼠标流这么说.net还是稍微方便了那么一点点。。。
嗯嗯,今天要早点休息了,不能想太多。
明天中午早点起来先一起吃顿饭,然后休息下准备好好考试了~
希望大家都能够通过:)

2 条评论:

匿名 说...

恩。。。以后要用到连接数据库就来你这拉了。。。

Stellahhhh 说...

好好叫,你要用手动连接啊,先拖个datagrid控键,再用那个dataadpater啊。。。