visual c++ 2015 使用ado.net例程, 向数据库表插入一行并查询行数。

ado.net 同时被 3 个专栏收录
1 篇文章 0 订阅
1 篇文章 0 订阅
2 篇文章 0 订阅

数据库名为:test 

表名为: idvalues, 有两个字段:id int, name varchar(50)


代码在visual c++ 2015 下编辑通过。

#include <stdio.h>

#include <tchar.h>
#include "stdafx.h"

#using <mscorlib.dll>

#using <System.Dll>
#using <System.Data.Dll>


using namespace System;
using namespace System::Data;
using namespace System::Data::Common;
using namespace System::Data::SqlClient;


int main()
{
System::Console::Write("begin\n");
SqlConnection ^conn = gcnew SqlConnection();
conn->ConnectionString = L"data source=(local);integrated security=true;initial catalog=test;";
try {
conn->Open();
}
catch (SqlException ^ex) {
System::Console::Write("Error:"+ex->Message+"\n");
goto error;
}
SqlCommand ^cmd = gcnew SqlCommand();
cmd->CommandType = CommandType::Text;
cmd->Connection = conn;
cmd->CommandText = "Declare @id int; SELECT @id=MAX(ID) FROM idValues; INSERT INTO idValues(id, name)VALUES(ISNULL(@id,0)+1, 'name'+CAST(@id as varchar(12)))";
int rows =cmd->ExecuteNonQuery();
System::Console::WriteLine("affected Rows: "+ rows.ToString());


cmd->CommandText = L"select count(1) from idValues";
Object ^o = cmd->ExecuteScalar();
System::Console::WriteLine("idValues has "+ o->ToString() + " rows now.");


System::Console::WriteLine("end");
goto exitor;
error:
exitor:
System::Console::Read();
conn->Close();
delete conn;
delete cmd;
    return 0;
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值