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
mysql是有c语言接口的,安装相应库后就可以链接了,一般连接mysql的函数是mysql_connect或者mysql_real_connect(大概就是这么拼的吧。。。)可以使用mysql_query执行sql语句//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路径//在工程设置-》链接》库模块中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn
MYSQL_RES *res
MYSQL_ROW row
char *server ="localhost"
char *user ="root"
char *password=""
char *database="test"
char sql[1024]="select * from chinaren"
conn=mysql_init(NULL)
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn))
exit(1)
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn))
exit(1)
}
res=mysql_use_result(conn)
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2])
}
mysql_free_result(res)
mysql_close(conn)
}
===============================
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定义数据库 *** 作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*handle //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result //查询结果集,结构类型
MYSQL_FIELD *field//包含字段信息的结构
MYSQL_ROW row //存放一行查询结果的字符串数组
char querysql[160] //存放查询sql语句字符串
//初始化
mysql_init(&mysql)
//连接数据库
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql))
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]))
//查询数据库
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle))
}
//存储结果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle))
}
printf("number of fields returned: %d\n",mysql_num_fields(result))
//读取结果集的内容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) )
}
//释放结果集
mysql_free_result(result)
//关闭数据库连接
mysql_close(handle)
system("PAUSE")
//为了兼容大部分的编译器加入此行
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)