1:使用配置的数据库连接串,创建数据库连接 Connection 对象
2:构建 *** 作的sql语句
3:定义command对象
4:打开数据连接
5:执行命令
举一个例子,删除 *** 作
public class StudentService
{
//从配置文件中读取数据库连接字符串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString()
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString()
AdoNetModels.Student model = new Student()
#region 删除数据1
public int DeleteStudent(int stuID)
{
int result = 0
// 数据库连接 Connection 对象
SqlConnection connection = new SqlConnection(connString)
// 构建删除的sql语句
string sql = string.Format("Delete From Student Where stuID={0}", stuID)
// 定义command对象
SqlCommand command = new SqlCommand(sql, connection)
try
{
connection.Open()
result = command.ExecuteNonQuery() // 执行命令
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
finally
{
connection.Close()
}
return result
}
#endregion
分类: 电脑/网络 >>程序设计 >>其他编程语言问题描述:
C或者以C++与SQL应该如何连接,谢谢!
解析:
用odbc或db-libary给你一小段db-library程序
int CreateProc(PDBPROCESS * dbproc,char * pwd,char * sname)
{
PLOGINREC login
char sname[20]
unsigned short num
memset(sname,0,sizeof(sname))
login=dblogin()
DBSETLUSER(login,"sa")
if(strlen(pwd)>0)
{
DBSETLPWD(login,pwd)
}
DBSETLVERSION(login, DBVER60)
dbprocerrhandle(login,err_handler)
dbprocmsghandle(login,msg_handler)
dbserverenum(LOC_SEARCH,sname,sizeof(sname),&num)
DBSETLTIME(login,20)
*dbproc=dbopen(login,sname)
if(*dbproc == NULL){
return ERR
}
dbuse(*dbproc,"ccenter")
dbfreelogin(login)
return OK
}
int check_grp_no(int grp_no, unsigned int * *** all_len)
{
int res=0
int t=0
dbcancel(dbproc)
dbfcmd(dbproc," select *** all_len from grp_table where grp_no=%d",grp_no)
res=dbsqlexec(dbproc)
if((res=dbresults(dbproc))==SUCCEED){
dbbind(dbproc,1,INTBIND,0,(unsigned char *) *** all_len)
while(dbnextrow(dbproc)!=NO_MORE_ROWS){
t++
return OK
}
}
return ERR
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)