一、 步骤:1.添加九种组件(实际可以减少)
2.组件间的关联
3.配置组件
4.编写代码
二、具体操作流程
1.添加组件:1个GroupBox(可以不要)、1个 ADOConnection、2个 ADOQuery、1个 DataSource、1个 DBGrid、6个 Button、3个 Label、3个 Edit、1个 Memo。
2.配置组件关系
(1)修改组件名称(按下图依次修改)
(2)组件配置
<1> 双击ADOConnection1组件 —>点击Build... —>选择Microsoft OLE DB Provider for SQL Server —>填写服务器名称、用户名称、密码、选择数据库 —>测试连接 —>确定。
注:服务器名称、用户名称、密码、选择数据库,可参考这篇文章
![]() | ![]() | |
![]() | ![]() |
<2> ADOQuery_show、 ADOQuery_ins 组件的 Connection属性 选择ADOConnection1
<3> DataSource1 组件的DataSet属性 选择ADOQuery_show
<4> DBGrid1 组件的DataSource属性 选择DataSource1
<5> 配置 DBGrid1 组件。属性介绍
Alignment:字段内容对齐方式 FieldName:字段名 Font —>color:字段内容字体颜色
Title下的Alignment:字段名对齐方式 Title —>Font —>color:字段名字体颜色
3.编写代码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "对SQLserver数据库的增删查改.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tfm1 *fm1;
AnsiString u_id1,username1,password1;
//---------------------------------------------------------------------------
__fastcall Tfm1::Tfm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::FormCreate(TObject *Sender)
{
if (ADOConnection1->Connected){
fm1->ADOQuery_show->Close(); fm1->ADOQuery_show->Open();
return;
}
else { fm1->Memo1 ->Lines ->Add("未连接到数据库!");}
}
//---------------------------------------------------------------------------
/*
SQLstr = "SELECT * FROM Table_1 WHERE (Table_1.x1='"+u_id+"' or Table_1.x2='"+username+"' or Table_1.x3='"+password+"');";
语句解析:"SELECT * FROM Table_1 WHERE (Table_1.x1='"+u_id+"' or Table_1.x2='"+username+"' or Table_1.x3='"+password+"');"
是SQL Server查询语句,将其赋值给 SQLstr变量,通过 fm1->ADOQuery_ins->SQL->Add(SQLstr); 语句进行查询
Table_1.x1:Table_1表的x1字段(字段相当于列) '"+u_id+"':自定义变量
Table_1.x1='"+u_id+"':将 u_id 变量内容赋值给 Table_1.x1 相当于查询 u_id 变量内容在不在 Table_1.x1字段下
*/
int Lookup()
{
//向SQL数据库查找数据
AnsiString SQLstr,SQLstr1,u_id,username,password;
u_id= fm1->Edit1->Text; username= fm1->Edit2->Text; password= fm1->Edit3->Text;
SQLstr = "SELECT * FROM Table_1 WHERE (Table_1.x1='"+u_id+"' and Table_1.x2='"+username+"' and Table_1.x3='"+password+"');";
fm1->ADOQuery_ins->Close(); fm1->ADOQuery_ins->SQL->Clear();
fm1->ADOQuery_ins->SQL->Add(SQLstr);
fm1->ADOQuery_ins->Open();
if(fm1->ADOQuery_ins->RecordCount){ return 0;}
else { return 1;}
}
/*
SQLstr ="INSERT INTO Table_1 (x1,x2,x3) ";
SQLstr+="VALUES (:suibian1,:suibian2,:suibian3);";
语句解析:"INSERT INTO Table_1 (x1,x2,x3) ";是SQL Server插入语句,
"VALUES (:suibian1,:suibian2,:suibian3);";是SQL Server赋值语句 ,
将其两条语句赋值给 SQLstr变量,通过 fm1->ADOQuery_ins->SQL->Add(SQLstr); 语句进行插入
fm1->ADOQuery_ins->Parameters->ParamByName("suibian1")->Value = u_id;是将 u_id 变量的值赋值给 suibian1
*/
void insert()
{
//向SQL数据库插入数据
AnsiString u_id,username,password;
u_id= fm1->Edit1->Text; username= fm1->Edit2->Text; password= fm1->Edit3->Text;
if(fm1->Edit1->Text!="" | fm1->Edit2->Text!="" | fm1->Edit2->Text!="" )
{
AnsiString SQLstr;
//查找 是否已经存在 u_id,username,password
if(Lookup() ==0) { ShowMessage("已存在");}
else if(Lookup() ==1)
{
SQLstr ="INSERT INTO Table_1 (x1,x2,x3) "; //插入语句
SQLstr+="VALUES (:suibian1,:suibian2,:suibian3);";
fm1->ADOQuery_ins->Close(); fm1->ADOQuery_ins->SQL->Clear();
fm1->ADOQuery_ins->SQL->Add(SQLstr);
fm1->ADOQuery_ins->Parameters->ParamByName("suibian1")->Value = u_id;//赋值语句
fm1->ADOQuery_ins->Parameters->ParamByName("suibian2")->Value = username;
fm1->ADOQuery_ins->Parameters->ParamByName("suibian3")->Value = password;
fm1->ADOQuery_ins->ExecSQL(); //执行修改
//显示SQL数据
fm1->ADOQuery_show->Close(); fm1->ADOQuery_show->Open();
}
//清空文本
fm1->Edit1->Text=""; fm1->Edit2->Text="";fm1->Edit3->Text="";
}
else if(fm1->Edit1->Text=="" ) {fm1->Memo1 ->Lines ->Add("u_id不能为空");}
else if(fm1->Edit2->Text=="" ) {fm1->Memo1 ->Lines ->Add("username不能为空");}
else if(fm1->Edit3->Text=="" ) {fm1->Memo1 ->Lines ->Add("password不能为空");}
}
void Delete()
{
//向SQL数据库删除数据
AnsiString u_id,username,password;
u_id= fm1->Edit1->Text; username= fm1->Edit2->Text; password= fm1->Edit3->Text;
if(fm1->Edit1->Text!="" | fm1->Edit2->Text!="" | fm1->Edit2->Text!="" )
{
AnsiString SQLstr;
//查找 是否已经存在 u_id,username,password
if(Lookup() ==0)
{
SQLstr = "DELETE FROM Table_1 WHERE (Table_1.x1='"+u_id+"' and Table_1.x2='"+username+"' and Table_1.x3='"+password+"');"; //删除语句
fm1->ADOQuery_ins->Close(); fm1->ADOQuery_ins->SQL->Clear();
fm1->ADOQuery_ins->SQL->Add(SQLstr);
fm1->ADOQuery_ins->ExecSQL();
//显示SQL数据
fm1->ADOQuery_show->Close(); fm1->ADOQuery_show->Open();
}
else if(Lookup() ==1) {fm1->Memo1 ->Lines ->Add("该记录不存在");}
//清空文本
fm1->Edit1->Text=""; fm1->Edit2->Text="";fm1->Edit3->Text="";
}
else if(fm1->Edit1->Text=="" ) {fm1->Memo1 ->Lines ->Add("u_id不能为空");}
else if(fm1->Edit2->Text=="" ) {fm1->Memo1 ->Lines ->Add("username不能为空");}
else if(fm1->Edit3->Text=="" ) {fm1->Memo1 ->Lines ->Add("password不能为空");}
}
void Modify()
{
//对SQL数据库修改数据
AnsiString SQLstr,u_id,username,password;
u_id= fm1->Edit1->Text; username= fm1->Edit2->Text; password= fm1->Edit3->Text;
//查找 是否已经存在 u_id,username,password
if(Lookup() ==1) //如果不存在,则修改
{
//查找 是否已经存在 u_id,username,password
SQLstr = "UPDATE Table_1 SET x1='"+u_id+"', x2='"+username+"', x3='"+password+"'WHERE (Table_1.x1='"+u_id1+"' and Table_1.x2='"+username1+"' and Table_1.x3='"+password1+"');";
fm1->ADOQuery_ins->Close(); fm1->ADOQuery_ins->SQL->Clear();
fm1->ADOQuery_ins->SQL->Add(SQLstr);
fm1->ADOQuery_ins->ExecSQL();
//清空文本
fm1->Edit1->Text=""; fm1->Edit2->Text=""; fm1->Edit3->Text="";
//显示SQL数据
fm1->ADOQuery_show->Close(); fm1->ADOQuery_show->Open();
u_id1 = ""; username1 = ""; password1 = "";
fm1->Button4 ->Enabled = false;
fm1->Memo1 ->Lines ->Add("修改成功");
}
else if(Lookup() ==0) {ShowMessage("该记录已存在");}
}
void __fastcall Tfm1::Button1Click(TObject *Sender)
{
if(Lookup() ==0) { fm1->Memo1 ->Lines ->Add("该记录已存在");}
else if(Lookup() ==1) { fm1->Memo1 ->Lines ->Add("该记录不存在");}
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::Button2Click(TObject *Sender)
{
insert();
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::Button3Click(TObject *Sender)
{
Delete();
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::Button4Click(TObject *Sender)
{
Modify();
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::Button5Click(TObject *Sender)
{
fm1->ADOQuery_show->Close(); fm1->ADOQuery_show->Open();
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::Button6Click(TObject *Sender)
{
fm1->Memo1 ->Clear(); fm1->Memo1 ->Lines ->Add("消息:");
}
//---------------------------------------------------------------------------
void __fastcall Tfm1::DBGrid1CellClick(TColumn *Column)
{
AnsiString tmpSS;
//tmpSS = Column->Field->CurValue;
tmpSS = Column->FieldName; //得到 字段名称
if(tmpSS == "x1" | tmpSS == "x2" | tmpSS == "x3") //向 上面的查询条件放
{
u_id1 = Column->Field->CurValue; // 数据库或数据表格中的某列(Column)具体字段(Field)的具体值(CurValue)
username1 = fm1->ADOQuery_show->FieldByName("x2")->AsAnsiString;
password1 = fm1->ADOQuery_show->FieldByName("x3")->AsAnsiString;
fm1->Button4 ->Enabled = true;
}
else return;
}
//---------------------------------------------------------------------------
4.编译代码并运行。按 F9键 或 Run
5.得到结果。
6.源码链接。可直接在桌面运行