关于连接Sqlserver

rongshida 2010-02-01 10:29:01
try
{
m_spConnection.CreateInstance( __uuidof(Connection) );
/* m_spConnection->Open(
_T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Demo.accdb"),
_T(""),
_T(""),
adModeUnknown );*/

m_spConnection->Open
(_T("driver={SQL Server};Server=127.0.0.1;DATABASE=;UID=sa;PWD=123"),"","",/*0*/adConnectUnspecified);

}
红色部分是原来的连接代码,绿色的是我改后的,
程序运行后(用绿色的)库中的记录都可显示,但添加记录时会出现Idispatch error #3092错误,但用原来的连接方式确不会出错,下面是添加记录代码
CString strName;
m_editName.GetWindowText( strName );
if( strName.IsEmpty() || strName.GetLength() >= 50 )
{
MessageBox(_T("姓名不可以为空或者姓名太长!") );
return;
}

CString strAge;
m_editAge.GetWindowText( strAge );
if( strAge.IsEmpty() )
{
MessageBox( _T("年龄不可为空") );
return;
}

int iAge = _tstoi( (LPCTSTR)strAge );
if( iAge<0 || iAge > 150 )
{
MessageBox( _T("年龄输入不合法:太大或太小") );
return;
}

// 添加到数据库中去
CString strCmd;
strCmd.Format( _T("insert into demo values( \"%s\", %d )"),
strName,
iAge );

_CommandPtr spCmd;
spCmd.CreateInstance( __uuidof(Command) );
spCmd->ActiveConnection = this->m_spConnection;
spCmd->CommandText = (LPCTSTR)strCmd;
try
{
spCmd->Execute( NULL, NULL, adCmdText );
}
catch( _com_error& e )
{
MessageBox( e.ErrorMessage() );
return;
}

谁明白给指出错误的地方
...全文
82 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rongshida 2010-02-02
  • 打赏
  • 举报
回复
呵呵,解决了,("insert into demo values( \"%s\", %d )"), 去掉转义字符就好使了,但是连access时没出错一切都正常换成sqlserver就出错,唉,不知为什么,也许是更严格了一些吧
多谢馒头的帮助,呵呵
marrco2005 2010-02-02
  • 打赏
  • 举报
回复
我的连接字符串时这样写的,在工程里面用的一直没问题, 你改一下试试

Provider=SQLOLEDB; Data Source=服务器地址; Initial Catalog=数据库名称; User ID=sa; Password=123;
rongshida 2010-02-02
  • 打赏
  • 举报
回复
sql语句没问题
marrco2005 2010-02-01
  • 打赏
  • 举报
回复
你把你的SQL语句放在 SQLserver里面 直接执行一下, 看嫩不能成功
rongshida 2010-02-01
  • 打赏
  • 举报
回复
m_pCon.CreateInstance(__uuidof(Connection)); 是两条”_”呀,编译能通过,且能运行,刷新代码也好使(能把库中记录显示在列表中),就是删除记录和添加记录的代码出错
crazyhan00 2010-02-01
  • 打赏
  • 举报
回复

	m_pCon.CreateInstance(__uuidof(Connection));  	// 创建连接实例,注意是两条”_”
m_pCon->Open("Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=XSCJ;Data Source= DELL-CRAZY","","",0); // 设置连接字符串



rongshida 2010-02-01
  • 打赏
  • 举报
回复
不行,还是那个错误
marrco2005 2010-02-01
  • 打赏
  • 举报
回复
那就把ADODB::去掉
rongshida 2010-02-01
  • 打赏
  • 举报
回复
ADODB::这个是哪来的?没定义呀
marrco2005 2010-02-01
  • 打赏
  • 举报
回复
ADODB::_RecordsetPtr m_spRecordset;
m_spRecordset.CreateInstance( __uuidof( ADODB::Recordset) );
m_spRecordset = m_spConnection->Execute( bstrSQL, NULL, ADODB::adCmdText) ;

用这个试一试

4,012

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧