我的团长我的团 云盘:编程实现访问SQL Server数据库

来源:百度文库 编辑:中科新闻网 时间:2024/05/09 11:56:20
我刚学SQL Server,老师布置用语言编写程序,用来访问SQL Server数据库,本人编程思想级差,所以向各位征集良方了。
PS:用JAVA、C++、C、VB四种语言编写均可,要求有图形界面,实现对数据库的增加、删除、查询功能即可。我需要的是完整的代码,包括SQL Server数据库实例,谢谢!
2楼的答案觉得很不错
但我看不出是什么类型的语言啊
哪位知道指点一下
还有其他程序吗?程序列出时请注明为何语言,谢谢!

这还不简单
只要学会其中的一种其它的都会
不过我只有ASP的

给你代码看吧.
#pragma once
#include "afx.h"
#import "C:\Program Files\Common Files\System\ado\msado15.dll" rename_namespace("AdoNs")rename("BOF","adoBOF")rename("EOF","adoEOF")
using namespace AdoNs;
//#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")

class CADOConn :public CObject
{
public:
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
public:
_RecordsetPtr GetRecordset(_bstr_t bstrSQL,_bstr_t DB_Name);
void ExitConnect();
BOOL OnInitADOConn(_bstr_t DB_Name);
BOOL Execute(_bstr_t bstrSQL,_bstr_t DB_Name);
public:
CADOConn(void);
public:
~CADOConn(void);
};
// GridImageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GridImage.h"
#include "GridImageDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridImageDlg dialog

CGridImageDlg::CGridImageDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGridImageDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGridImageDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGridImageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGridImageDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
DDX_GridControl(pDX, IDC_GRIDCTRL, m_GridCtrl);

}

BEGIN_MESSAGE_MAP(CGridImageDlg, CDialog)
//{{AFX_MSG_MAP(CGridImageDlg)
ON_NOTIFY(GVN_BEGINLABELEDIT, IDC_GRIDCTRL, OnGridSelChange )
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_INSERT, OnInsert)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_SAVE, OnSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridImageDlg message handlers

BOOL CGridImageDlg::OnInitDialog()
{
CDialog::OnInitDialog();

//获得主程序的路径
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
CString strCrePath; //在程序路径下创建一个文件夹
strCrePath.Format("%s%s",sPath,_T("\\临时图片\\"));
CreateDirectory(strCrePath,NULL);

pBuffer = NULL;
index = 1;
m_GridCtrl.SetColumnCount(7);
m_GridCtrl.SetFixedRowCount(1);

m_GridCtrl.SetItemText(0, 0, _T("图片编号"));
m_GridCtrl.SetItemText(0, 1, _T("桥梁编号"));
m_GridCtrl.SetItemText(0, 2, _T("图片数据"));
m_GridCtrl.SetItemText(0, 3, _T("图片类别名称"));
m_GridCtrl.SetItemText(0, 4, _T("图片后缀"));
m_GridCtrl.SetItemText(0, 5, _T("拍摄日期"));
m_GridCtrl.SetItemText(0, 6, _T("图片描述"));

CString strSQL;
CADOConn m_CAdoConn;
_RecordsetPtr pRecordset;
strSQL=_T("select * from 基本信息图片");
pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("linji"));
int m_ncount ;
m_ncount=pRecordset->GetRecordCount();
m_GridCtrl.SetRowCount(m_ncount+1);
//设置图片数据栏灰色
while (m_ncount > 0)
{
m_GridCtrl.SetItemBkColour(m_ncount,2,RGB(236,233,216));
m_GridCtrl.SetItemText(m_ncount,2,_T(""));
m_ncount--;
}

int row = 1;
while (!pRecordset->adoEOF)
{
CString str;
FieldsPtr pFields = pRecordset->Fields;

FieldPtr pField = pFields->GetItem(0L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 0L,(char*)(_bstr_t)pField->Value);
pField = pFields->GetItem(1L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 1L, (char*)(_bstr_t)pField->Value);
pField = pFields->GetItem(2L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 3L,(char*)(_bstr_t)pField->Value);
pField = pFields->GetItem(3L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 4L, (char*)(_bstr_t)pField->Value);
pField = pFields->GetItem(5L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 5L, (char*)(_bstr_t)pField->Value);
pField = pFields->GetItem(6L);
if (pField->Value.vt != VT_NULL)
m_GridCtrl.SetItemText(row, 6L, (char*)(_bstr_t)pField->Value);

//读取图片数据
nSize = pRecordset->GetFields()->GetItem("图片数据")->ActualSize;
if (nSize <= 0)
{
m_GridCtrl.SetItemText(row,2L,_T(""));
}
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = pRecordset->GetFields()->GetItem("图片数据")->GetChunk(nSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(pBuffer = new BYTE [nSize+1]) ///重新申请必要的存储空间
{
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pBuffer,pBuf,nSize); ///复制数据到缓冲区
SafeArrayUnaccessData (varBLOB.parray);
}
}
//保存文件到临时图片
CString strExt;
strExt.Format("%s%s",_T("."),m_GridCtrl.GetItemText(row,4));
CString str;
str.Format("%s%d%s",strCrePath,index,strExt); //文件名和后缀名
CFile outFile(str,CFile::modeCreate|CFile::modeWrite);
outFile.Write(pBuffer,nSize);
CString strindex;
strindex.Format("%d",index);
m_GridCtrl.SetItemText(row,2,strindex);
index++;
}
row++;
pRecordset->MoveNext();
}
m_CAdoConn.ExitConnect();
return TRUE; // return TRUE unless you set the focus to a control
}

void CGridImageDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CGridImageDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGridImageDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CGridImageDlg::OnAdd()
{
//添加一行
int rowcount = m_GridCtrl.GetRowCount();
m_GridCtrl.SetRowCount(rowcount+1);
//设置该行属性,颜色,不可以修改,字段
m_GridCtrl.SetItemBkColour(rowcount,2,RGB(236,233,216));
CString strindex;
strindex.Format("%d",index);
m_GridCtrl.SetItemText(rowcount,2,strindex);
index++;
//添加日期数据
CTime nTime = CTime::GetCurrentTime();
CString str;
str.Format("%d-%d-%d",nTime.GetYear(),nTime.GetMonth(),nTime.GetDay());
m_GridCtrl.SetItemText(rowcount, 5, str);
//设置光标,自动滚动到最后一行
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = 20000;
m_GridCtrl.SetScrollInfo(SB_VERT,&si,TRUE);
m_GridCtrl.SetFocusCell(rowcount,0);

}

void CGridImageDlg::OnInsert()
{
int row = m_GridCtrl.GetFocusCell().row;
if (row==-1)
{
MessageBox(_T("请选择插入位置"));
return;
}
int rowcount = m_GridCtrl.GetRowCount();
m_GridCtrl.InsertRow("",row);
m_GridCtrl.SetFocusCell(row,0);
m_GridCtrl.SetRowHeight(row,25);
//设置该行属性,颜色,字段
m_GridCtrl.SetItemBkColour(row,2,RGB(236,233,216));
CString strindex;
strindex.Format("%d",index);
m_GridCtrl.SetItemText(row,2,strindex);
index++;
//添加日期数据
CTime nTime = CTime::GetCurrentTime();
CString str;
str.Format("%d-%d-%d",nTime.GetYear(),nTime.GetMonth(),nTime.GetDay());
m_GridCtrl.SetItemText(row, 5, str);
m_GridCtrl.Invalidate();
}

void CGridImageDlg::OnDelete()
{
int row = m_GridCtrl.GetFocusCell().row;
if( row == -1)
{
MessageBox(_T("请选择要删除的行"));
return;
}
if (row > 0)
{
m_GridCtrl.DeleteRow(row);
}
//设置光标位置
if (m_GridCtrl.GetRowCount() > row)
m_GridCtrl.SetFocusCell(row,0);
else
m_GridCtrl.SetFocusCell(m_GridCtrl.GetRowCount()-1,0);

m_GridCtrl.Invalidate();
}

void CGridImageDlg::OnSave()
{
CString strSQL;
CADOConn m_CAdoConn;
_RecordsetPtr pRecordset;
//先删除
strSQL=_T("delete 基本信息图片");
pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("linji"));
//后添加
strSQL=_T("select * from 基本信息图片");
pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("linji"));
int m_Rcount = 1;
while (m_Rcount < m_GridCtrl.GetRowCount())
{
pRecordset->AddNew();
if(m_GridCtrl.GetItemText(m_Rcount,0)!="")
pRecordset->Fields->GetItem(_variant_t("图片编号"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,0));
if(m_GridCtrl.GetItemText(m_Rcount,1)!="")
pRecordset->Fields->GetItem(_variant_t("桥梁编号"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,1));

//获得文件名
CString m_sname;
m_sname.Format("%s",m_GridCtrl.GetItemText(m_Rcount,2));
//获取扩展名
CString m_sExt;
m_sExt.Format("%s%s",_T("."),m_GridCtrl.GetItemText(m_Rcount,4));
//获得主程序的路径
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
//获取图片数据
CFile f;
CString FilePathName;
FilePathName.Format("%s%s%s%s",sPath,_T("\\临时图片\\"),m_sname,m_sExt); //文件名和后缀名
CFileException e;
if(f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个文件
{

int nSize = f.GetLength(); //先得到文件长度
BYTE * pBuffer = new BYTE [nSize]; //按文件的大小在堆上申请一块内存

if (f.Read(pBuffer, nSize) > 0 ) //把文件读到pBuffer(堆上申请一块内存)
{ // +----------------------------------------------
BYTE *pBuf = pBuffer; ///下面这一大段是把pBuffer里的数据放到库中
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nSize;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nSize; i++)
SafeArrayPutElement (psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
pRecordset->GetFields()->GetItem("图片数据")->AppendChunk(varBLOB);
}
delete [] pBuffer; //删掉堆上申请的那一块内存
pBuf=0; //以防二次乱用
f.Close();
}
}
if(m_GridCtrl.GetItemText(m_Rcount,3)!="")
pRecordset->Fields->GetItem(_variant_t("图片类别名称"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,3));
if(m_GridCtrl.GetItemText(m_Rcount,4)!="")
pRecordset->Fields->GetItem(_variant_t("图片后缀"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,4));
if(m_GridCtrl.GetItemText(m_Rcount,5)!="")
pRecordset->Fields->GetItem(_variant_t("拍摄日期"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,5));
if(m_GridCtrl.GetItemText(m_Rcount,6)!="")
pRecordset->Fields->GetItem(_variant_t("图片描述"))->Value=_bstr_t(m_GridCtrl.GetItemText(m_Rcount,6));
m_Rcount++;
pRecordset->Update();
}
m_CAdoConn.ExitConnect();

}

void CGridImageDlg::OnCancel()
{
//删除过程:先删除文件夹的文件,再删除文件夹
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
CString strDelPath;
strDelPath.Format("%s%s",sPath,_T("\\临时图片"));//获得临时图片的路径

CFileFind tempFind;
CString temp;
temp=strDelPath+"\\*.*";//遍历临时图片文件夹下所有文件
BeginWaitCursor();
BOOL IsFinded=tempFind.FindFile(temp);
while(IsFinded)
{
IsFinded=tempFind.FindNextFile();
if(!tempFind.IsDots())
{
if(tempFind.IsDirectory())
{
CString tempStr;
tempStr=tempFind.GetFileName();
tempStr=strDelPath+"\\"+tempStr;
DeleteFile(tempStr);
}
else
{
CString tempStr;
tempStr=strDelPath+"\\"+tempFind.GetFileName();
DeleteFile(tempStr);
}
}
}
tempFind.Close();
if(!RemoveDirectory(strDelPath))
{
MessageBox("删除目录失败!","警告信息",MB_OK);
}
//关闭窗口

CDialog::OnCancel();
}

//DEL BOOL CGridImageDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
//DEL {
//DEL if (wParam ==IDC_GRIDCTRL)
//DEL {
//DEL CCellID focuscell;
//DEL focuscell = m_GridCtrl.GetFocusCell();
//DEL
//DEL int row = focuscell.row;
//DEL int col = focuscell.col;
//DEL if ((col == 2)&&(row > 0))
//DEL {
//DEL dlgData.m_nrow = row;
//DEL dlgData.m_sname = m_GridCtrl.GetItemText(row,2);//获得索引名
//DEL dlgData.m_OutFileName = m_GridCtrl.GetItemText(row,3); //获得文件名
//DEL dlgData.m_sEXt = m_GridCtrl.GetItemText(row,4); //获得扩展名
//DEL if (dlgData.m_sname == "") //如果选中的行没有图片编号
//DEL {
//DEL CString strIndex1;
//DEL strIndex1.Format("%d",index);
//DEL m_GridCtrl.SetItemText(dlgData.m_nrow+1,2,strIndex1);
//DEL CString strIndex2;
//DEL strIndex2.Format("%d",index);
//DEL dlgData.m_sname = strIndex2;
//DEL index++;
//DEL }
//DEL if (dlgData.DoModal()==IDOK)
//DEL {
//DEL //如果浏览文件,获得文件名及扩展名
//DEL if (dlgData.FileTitle!=""||dlgData.FileEXt!="")
//DEL {
//DEL m_GridCtrl.SetItemText(row,3,dlgData.FileTitle);
//DEL m_GridCtrl.SetItemText(row,4,dlgData.FileEXt);
//DEL dlgData.FileTitle = "";
//DEL dlgData.FileEXt = "";
//DEL }
//DEL }
//DEL }
//DEL
//DEL }
//DEL *pResult = 1;
//DEL return CDialog::OnNotify(wParam, lParam, pResult);
//DEL }
void CGridImageDlg::OnGridSelChange(NMHDR * pNotifyStruct, LRESULT * result )
{
NM_GRIDVIEW* pNmgv = (NM_GRIDVIEW*)pNotifyStruct;
*result = 0;
//获得第3列,某行(首行除外)
if ((pNmgv->iColumn == 2)&&(pNmgv->iRow != 0))
{
dlgData.m_nrow = pNmgv->iRow-1;
dlgData.m_sname = m_GridCtrl.GetItemText(pNmgv->iRow,2);//获得索引名
dlgData.m_OutFileName = m_GridCtrl.GetItemText(pNmgv->iRow,3); //获得文件名
dlgData.m_sEXt = m_GridCtrl.GetItemText(pNmgv->iRow,4); //获得扩展名
if (dlgData.m_sname == "") //如果选中的行没有图片编号
{
CString strIndex;
strIndex.Format("%d",index);
m_GridCtrl.SetItemText(dlgData.m_nrow+1,2,strIndex);
dlgData.m_sname = strIndex;
index++;
}
if (dlgData.DoModal()==IDOK)
{
//如果浏览文件,获得文件名及扩展名
if (dlgData.FileTitle!=""||dlgData.FileEXt!="")
{
m_GridCtrl.SetItemText(dlgData.m_nrow+1,3,dlgData.FileTitle);
m_GridCtrl.SetItemText(dlgData.m_nrow+1,4,dlgData.FileEXt);
dlgData.FileTitle = "";
dlgData.FileEXt = "";
}
}
}
}