数据库连接部分9161李佳.docx

上传人:b****3 文档编号:12903621 上传时间:2023-04-22 格式:DOCX 页数:20 大小:356.08KB
下载 相关 举报
数据库连接部分9161李佳.docx_第1页
第1页 / 共20页
数据库连接部分9161李佳.docx_第2页
第2页 / 共20页
数据库连接部分9161李佳.docx_第3页
第3页 / 共20页
数据库连接部分9161李佳.docx_第4页
第4页 / 共20页
数据库连接部分9161李佳.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

数据库连接部分9161李佳.docx

《数据库连接部分9161李佳.docx》由会员分享,可在线阅读,更多相关《数据库连接部分9161李佳.docx(20页珍藏版)》请在冰豆网上搜索。

数据库连接部分9161李佳.docx

数据库连接部分9161李佳

四川农业大学实验实习报告

专业:

计科班级:

1班姓名:

李佳学号:

20119161

数据库连接部分

——实训一用户验证

一、实验目的:

1、掌握使用ADO.NET对SQLserver2000的连接。

2、掌握ADO。

NET连接字符串的书写。

3、掌握ADO。

NET对数据库的操作原理。

二、实验内容:

实训1、用户验证

代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespaceSaleManager

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

this.CenterToScreen();

this.lbError.Visible=false;

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

stringUserID=this.tbUserName.Text.Trim();

stringUserPwd=this.tbPassword.Text.Trim();

SqlConnectionconn=newSqlConnection();

conn.ConnectionString="initialCatalog=Sales;DataSource=WIN-6D2TP2142TR;integratedSecurity=True";

conn.Open();

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

cmd.CommandText="SelectUserNameFromusersWhereUserID='"+UserID+"'AndPass_word='"+UserPwd+"'";

SqlDataReaderdr;

dr=cmd.ExecuteReader();

if(dr.Read())

{

MessageBox.Show(dr.GetString(0).ToString()+"为合法用户");

}

else

{

this.lbError.Visible=true;

this.lbError.Text="你输入的用户名或者密码错误,请重新输入!

";

}

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

this.Close();

}

}

}

运行的结果如下:

单击“登录”,结果如下:

——实训二添加数据

实训目的:

1、掌握使用ADO.NET对SQLSERVER2000的连接

2、掌握ADO.NET连接字符串的书写

3、掌握ADO.NET对数据库的操作原理

4、掌握利用ADO.NET向数据库中添加数据

实训内容:

1、在SQLSERVER2000数据库:

Sales中完成本次实验

2、使用的数据库表为:

Sales

3、实现对新销售人员信息的添加过程

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespacesalemanager

{

publicpartialclassAddSaler:

Form

{

publicAddSaler()

{

InitializeComponent();

}

privatevoidlabel1_Load(objectsender,EventArgse)

{

this.CenterToScreen();

}

privatevoidbtAdd_Click(objectsender,EventArgse)

{

stringS_id=this.tbSid.Text.Trim();

stringS_name=this.tbSname.Text.Trim();

stringS_Sex=this.tbSex.Text.Trim();

stringS_CSRQ=this.tbCSRQ.Text.Trim();

stringS_GYRQ=this.tbGYRQ.Text.Trim();

stringS_Address=this.tbAddress.Text.Trim();

stringS_Tel=this.tbTel.Text.Trim();

stringS_Note=this.tbNotes.Text.Trim();

SqlConnectionconn=newSqlConnection();

conn.ConnectionString="initialCatalog=Sales;DataSource=PC-20130224GNLO;integratedSecurity=True";

conn.Open();

SqlCommandcmd=newSqlCommand();

cmd.CommandType=CommandType.Text;

cmd.CommandText="insertintosalervalues('"+S_id+"','"+S_name+"','"+S_Sex+"','"+S_CSRQ+"','"+S_GYRQ+"','"+S_Address+"','"+S_Tel+"','"+S_Note+"')";

try

{

inttest;

test=cmd.ExecuteNonQuery();

MessageBox.Show(test.ToString()+"条记录已经成功写入数据库!

","提示");

}

catch

{

MessageBox.Show("数据写入失败,请重新添加!

","提示");

}

conn.Close();

}

privatevoidbtExit_Click(objectsender,EventArgse)

{

this.Close();

}

privatevoidtbNote_TextChanged(objectsender,EventArgse)

{}

privatevoidbtExit_Click_1(objectsender,EventArgse)

{

this.Close();

}

privatevoidtbTel_TextChanged(objectsender,EventArgse)

{}

}

}

运行结果如下所示:

单击“添加数据”按钮,结果如下:

 

——实训三填充数据集

目的:

1、掌握数据集的创建过程

2、掌握数据适配器的创建与使用

3、掌握使用数据适配器对数据集的填充

内容:

1、在sqlserver数据库:

sales完成本次实验

2、使用到的数据表为:

salers

3、实现对销售人员信息显示过程

——实训四DataGridView控件的应用

目的:

1、掌握使用DataGridView控件绑定数据集的数据

2、掌握DataGridView控件的CellClick事件

内容:

1、将Sales数据库附加到Sqlserver数据中;

2、在VS中打开Windows应用程序项目:

SaleManage

3、对DataGridView控件的CellClick事件的实现过程

——实训五对数据的操作

1、掌握对数据集数据的操作

2、掌握数据集行的创建与修改操作

内容:

1、将sales数据库附加到sqlserver数据中;

2、在VS中打开Windows应用程序项目:

SaleManage

3、对数据集中的数据进行操作

所有代码如下所示:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespaceSaleManage1

{

publicpartialclassSeeSaler:

Form

{

StringZT="";

publicSeeSaler()

{

InitializeComponent();

}

privatevoidSetTextBox(Booleanb)

{

this.tbSid.ReadOnly=b;

this.tbSname.ReadOnly=b;

this.tbSex.ReadOnly=b;

this.tbCSRQ.ReadOnly=b;

this.tbGYRQ.ReadOnly=b;

this.tbAddress.ReadOnly=b;

this.tbTel.ReadOnly=b;

this.tbNotes.ReadOnly=b;

}

privatevoidSetButton(Booleanb)

{

this.btAdd.Enabled=b;

this.btEdit.Enabled=b;

this.btSave.Enabled=!

b;

this.btQuit.Enabled=!

b;

this.btDel.Enabled=b;

this.btExit.Enabled=b;

}

privatevoidsplitContainer1_Panel2_Paint(objectsender,PaintEventArgse)

{}

//该方法完成:

当窗体运行时,就将销售员的所有信息显示在DataGridView控件中

privatevoidSeeSaler_Load(objectsender,EventArgse)

{

SetTextBox(true);

SetButton(true);

SqlConnectionconn=newSqlConnection();

conn.ConnectionString="initialCatalog=Sales;DataSource=PC-20130224GNLO;integratedSecurity=True";

conn.Open();

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandText="Select编号,姓名,性别,出生日期,雇佣日期,地址,电话,备注信息fromSaler";

SqlDataAdapterdaSaler=newSqlDataAdapter();

daSaler.SelectCommand=cmd;

daSaler.Fill(dsSaler,"Saler");

this.SalerGridView1.DataSource=this.dsSaler.Tables["Saler"].DefaultView;

}

privatevoidSalerGridView1_CellContentClick(objectsender,DataGridViewCellEventArgse)

{}

privatevoidSalerGridView1_CellContentClick_1(objectsender,DataGridViewCellEventArgse)

{}

privatevoidSalerGridView1_CellClick_1(objectsender,DataGridViewCellEventArgse)

{

this.tbSid.Text=this.SalerGridView1.CurrentRow.Cells[0].Value.ToString();

this.tbSname.Text=this.SalerGridView1.CurrentRow.Cells[1].Value.ToString();

this.tbSex.Text=this.SalerGridView1.CurrentRow.Cells[2].Value.ToString();

this.tbCSRQ.Text=this.SalerGridView1.CurrentRow.Cells[3].Value.ToString();

this.tbGYRQ.Text=this.SalerGridView1.CurrentRow.Cells[4].Value.ToString();

this.tbAddress.Text=this.SalerGridView1.CurrentRow.Cells[5].Value.ToString();

this.tbTel.Text=this.SalerGridView1.CurrentRow.Cells[6].Value.ToString();

this.tbNotes.Text=this.SalerGridView1.CurrentRow.Cells[7].Value.ToString();

}

privatevoidbtAdd_Click(objectsender,EventArgse)

{

SetTextBox(false);

SetButton(false);

this.tbSid.Text="";

this.tbSname.Text="";

this.tbSex.Text="";

this.tbCSRQ.Text="";

this.tbGYRQ.Text="";

this.tbAddress.Text="";

this.tbTel.Text="";

this.tbNotes.Text="";

ZT="Add";

}

privatevoidbtEdit_Click(objectsender,EventArgse)

{

SetTextBox(false);

SetButton(false);

ZT="Edit";

}

privatevoidbtDel_Click(objectsender,EventArgse)

{

stringid=this.tbSid.Text.Trim();

DialogResulttest=MessageBox.Show("你确定要删除吗?

","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

if(test==DialogResult.Yes)

{

SqlConnectionconn=newSqlConnection("initialCatalog=Sales;DataSource=PC-20130224GNLO;integratedSecurity=True");

conn.Open();

SqlCommandcmd=newSqlCommand("DeleteSalerWheresalerID='"+id+"'",conn);

cmd.ExecuteNonQuery();

SeeSaler_Load(sender,e);

conn.Close();

}

}

privatevoidbtSave_Click(objectsender,EventArgse)

{

stringsqlstring="";

switch(ZT)

{

case"Add":

DataRowhang=this.dsSaler.Tables["Saler"].NewRow();

hang["编号"]=this.tbSid.Text.Trim();

hang["姓名"]=this.tbSname.Text.Trim();

hang["性别"]=this.tbSex.Text.Trim();

hang["出生日期"]=this.tbGYRQ.Text.Trim();

hang["雇佣日期"]=this.tbAddress.Text.Trim();

hang["地址"]=this.tbTel.Text.Trim();

hang["电话"]=this.tbNotes.Text.Trim();

hang["备注信息"]=this.tbCSRQ.Text.Trim();

this.dsSaler.Tables["Saler"].Rows.Add(hang);

sqlstring="InsertintoSalersValues('"+tbSid.Text+"','"+tbSname.Text+"''"+tbSex.Text+"','"+tbCSRQ.Text+"','"+tbGYRQ.Text+"','"+tbAddress.Text+"','"+tbTel.Text+"','"+tbNotes.Text+"')";

break;

case"Edit":

sqlstring="UpdateSalerSetSalename='"+tbSname.Text+"',Sex='"+tbSex.Text+"',birthday+'"+tbGYRQ.Text+"',HireDate='"+tbGYRQ.Text+"'Address='"+tbAddress.Text+"',Telephone='"+tbTel.Text+"'Notes='"+tbNotes.Text+"'whereSaleId+'"+tbSid.Text+"'";

break;

}

try

{

SqlConnectionconn=newSqlConnection();

conn.ConnectionString="initialCatalog=Sales;DataSource=PC-20130224GNLO;integratedSecurity=True";

conn.Open();

SqlCommandcmd=newSqlCommand(sqlstring,conn);

cmd.ExecuteNonQuery();

conn.Close();

MessageBox.Show("本次数据操作成功!

","提示");

}

catch

{

MessageBox.Show("本次数据操作失败!

","提示");

}

}

privatevoidbtQuit_Click(objectsender,EventArgse)

{

SetTextBox(true);

SetButton(true);

}

privatevoidbtExit_Click(objectsender,EventArgse)

{

this.Close();

}

}

}

实训三结果显示如下:

实训四显示结果如下所示:

实训五运行结果如下所示:

 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1