您的位置:首页 > 数据库

实现VSTO操作WORD之三 --------实现WORD与数据库相互操作

2007-02-08 18:08 477 查看
上一次做了一些简单的页面操作,是有关于界面的,下面我们来进行复杂点的操作,对数据库进行操作

下面开始:

在开始前,我先把下面用到的表结构写出来

表名:person 字段 id int , name varchar ,age int ,sex varchar

如果表和代码有什么不规范的地方请谅解,我只是做一个简单的测试

首先创建一个新的OFFICE智能文档,创建完成后点击工具条上面的数据(shift+alt+D),我们来添加创建一个新的数据源,然后选择数据库--点击下一步--选择字符串的连接(我们点新建一个连接)





然后会出现一个选择数据库的对话框,我们在服务器名打上localhost,也可以选择网络里的!(如果时SQL2005好像只可以打localhost,SQL2000也可以打“.”)之后就是选择数据库了,这里我选TEST



点击确定后点击下一步,然后会出现你选的数据库里的表,视图,存储过程,函数。我们选择表PERSON点击下一步。之后我们程序的左边里多了XSD等文件,这是这个表的架构和一些相关的文件。右边多了一个DATASET以及相应的表和字段



下面我们就可以对表进行操作了~我们先将表person拖到WORD工作区,放开后,会自动的出现一个GRIDVIEW控件,它已经把表里的数据给绑定好了,自己调节一下大小就可以运行看看效果了



其实和以前的没什么区别吧,好了,下面我们在下面拖放4个label 和4个textbox和两个按钮,用来选择后

显示和添加的时候输入数据的。现在我们把WORD关闭掉来拖放控件,等拖放完成后我们就来编写相应的代码



首先我们先来实现label的显示数据,我们要在personDataGridView_Click的事件中添加代码:


private void personDataGridView_Click(object sender, EventArgs e)




...{


//this.label2.Text = this.personDataGridView[


this.label1.Text = this.personDataGridView[0, this.personDataGridView.CurrentRow.Index].Value.ToString();


this.label2.Text = this.personDataGridView[1, this.personDataGridView.CurrentRow.Index].Value.ToString();


this.label3.Text = this.personDataGridView[2, this.personDataGridView.CurrentRow.Index].Value.ToString();


this.label4.Text = this.personDataGridView[3, this.personDataGridView.CurrentRow.Index].Value.ToString();


}

可能和别的方法或属性有点不一样,不过一定是有的,在于找,呵呵。这样当我们点击gridview时就会显示相应

的数据了,下面我们来实现按钮。连接数据库和WINFORM,ASP.NET都一样,建了一个类将代码加进去


public static string conn = "Data Source=(local);initial catalog =test;user id=sa;pwd=";




public bool DeleteData( int id )




...{


SqlConnection strconn= new SqlConnection (conn);


SqlCommand command = new SqlCommand("Delete person where id=" + id + "", strconn);


try




...{


strconn.Open();


int i= command.ExecuteNonQuery();


if (i > 0)




...{


return true;


}


else




...{


return false;


}


}


catch (Exception ex)




...{


throw new Exception(ex.Message);


}


finally




...{


strconn.Close();




}


}




public bool InsertData(int id,string name,int age,string sex)




...{


SqlConnection strconn = new SqlConnection(conn);


SqlCommand command = new SqlCommand("insert into person values("+id+",'"+name+"',"+age+",'"+sex+"') ", strconn);


try




...{


strconn.Open();


int i = command.ExecuteNonQuery();


if (i > 0)




...{


return true;


}


else




...{


return false;


}


}


catch (Exception ex)




...{


throw new Exception(ex.Message);


}


finally




...{


strconn.Close();


}


}

之后我们点击删除按钮把下面代码加进去:我的类名叫database ^_^


database del = new database();


if (del.DeleteData(int.Parse(this.label1.Text)))




...{


MessageBox.Show("成功!!!!!");


this.personTableAdapter.Fill(testDataSet.person);






}


else




...{


MessageBox.Show("失败!!!!!");




}

添加按钮里的代码为:


if (this.textBox4.Text != "")




...{


database add = new database();


add.InsertData(int.Parse(this.textBox1.Text), this.textBox2.Text, int.Parse(this.textBox3.Text), this.textBox4.Text);


MessageBox.Show("添加成功!!!!");


this.personTableAdapter.Fill(testDataSet.person);




}


else




...{


MessageBox.Show("添加失败!!!!!");




}

值得注意的是当删除或添加完数据后,没有重新绑定的方法,只能是将TABLEADAPTER重新FILL一下表,就可以达到重新绑定的效果,VSTO的数据对象就是这样定义的,在这里不多说概念了,到这里我们就实现了对数据库的简单操作了。。谢谢大家的支持
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: