课程设计 使用asp.net,采用Access数据库,实现数据库表的基本操作,,如插入、删除、查询、更改等....很急
1个回答
展开全部
1.insert
private void NewNode(String type, int nodeid, String nodeText)
{//判断是新增树节点,还是子节点.
if (nodeid < 1) return;
String sql = "insert into ut_title(titleid,titlename,parentid) values(@titleid,@titlename,@parentid)";
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@titleid", nodeid);
cmd.Parameters.AddWithValue("@titlename", String.Empty.Equals(nodeText) ? "-新的标题-" + DateTime.Now.ToString("yyyyMMddHHmmss") : nodeText);
if ("sub".Equals(type))
{
cmd.Parameters.AddWithValue("@parentid", this.selectnode.Tag.ToString());
}
else
{
cmd.Parameters.AddWithValue("@parentid", "0");
}
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
2.update
private void UpdateNode(String titlename, int currentID)
{
String sql = "update ut_title set titlename='" + titlename + "' where titleid=" + curTitleID;
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
3.delete
private void DeleteNode()
{
if (this.selectnode == null || this.selectnode.Tag == null) return;
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
if (cn.State != ConnectionState.Open)
cn.Open();
OleDbTransaction tran = cn.BeginTransaction();
try
{
OleDbCommand cmd;
String sql = "";
//没有文档内容也可以删除标题
sql = "delete from ut_title where titleid=" + this.selectnode.Tag.ToString() + "";
cmd = new OleDbCommand(sql, cn);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
this.treeView1.Nodes.Remove(this.selectnode);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
4.select
private void LoadImgDataToRichTextBox(int titleid)
{
//从数据库中读出数据
String sql = String.Format("select top 1 accessory,docdesc from ut_doc where titleid = @titleid");
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
OleDbParameter p0 = new OleDbParameter("@titleid", OleDbType.Integer);
p0.Value = titleid;
OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
da.SelectCommand.Parameters.Add(p0);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
this.txt_tag.Text = dt.Rows[0][1].ToString();
byte[] bWrite = (byte[])dt.Rows[0][0];
//将数组转换成stream
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
//将stream填充到RichTextBox
this.htmlRichTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
5.connection string:
public static String ConnectionString_unEncry
{
get
{
String str = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}\newapp.mdb", System.Windows.Forms.Application.StartupPath);
return str;
}
}
private void NewNode(String type, int nodeid, String nodeText)
{//判断是新增树节点,还是子节点.
if (nodeid < 1) return;
String sql = "insert into ut_title(titleid,titlename,parentid) values(@titleid,@titlename,@parentid)";
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@titleid", nodeid);
cmd.Parameters.AddWithValue("@titlename", String.Empty.Equals(nodeText) ? "-新的标题-" + DateTime.Now.ToString("yyyyMMddHHmmss") : nodeText);
if ("sub".Equals(type))
{
cmd.Parameters.AddWithValue("@parentid", this.selectnode.Tag.ToString());
}
else
{
cmd.Parameters.AddWithValue("@parentid", "0");
}
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
2.update
private void UpdateNode(String titlename, int currentID)
{
String sql = "update ut_title set titlename='" + titlename + "' where titleid=" + curTitleID;
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
3.delete
private void DeleteNode()
{
if (this.selectnode == null || this.selectnode.Tag == null) return;
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
if (cn.State != ConnectionState.Open)
cn.Open();
OleDbTransaction tran = cn.BeginTransaction();
try
{
OleDbCommand cmd;
String sql = "";
//没有文档内容也可以删除标题
sql = "delete from ut_title where titleid=" + this.selectnode.Tag.ToString() + "";
cmd = new OleDbCommand(sql, cn);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
this.treeView1.Nodes.Remove(this.selectnode);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
4.select
private void LoadImgDataToRichTextBox(int titleid)
{
//从数据库中读出数据
String sql = String.Format("select top 1 accessory,docdesc from ut_doc where titleid = @titleid");
OleDbConnection cn = new OleDbConnection(BizSvr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
OleDbParameter p0 = new OleDbParameter("@titleid", OleDbType.Integer);
p0.Value = titleid;
OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
da.SelectCommand.Parameters.Add(p0);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
this.txt_tag.Text = dt.Rows[0][1].ToString();
byte[] bWrite = (byte[])dt.Rows[0][0];
//将数组转换成stream
System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
//将stream填充到RichTextBox
this.htmlRichTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
5.connection string:
public static String ConnectionString_unEncry
{
get
{
String str = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}\newapp.mdb", System.Windows.Forms.Application.StartupPath);
return str;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询