本文介绍了如何在Visual C ++和MS SQLDB之间建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,Hi guys,我正在尝试编写Windows在Visual Studio 2017中关于数据库的表单应用程序。我创建了一个本地数据库文件,还添加了一个SQL查询文件,该文件旨在在此数据库中添加一行。我之前有使用C ++的经验,所以Windows表单部分对我来说并不那么困难。I am trying to write a Windows Form Application in Visual Studio 2017 regarding a database. I have created a local database file and also added a SQL query file which intent to add one row in this database. I have experience with C++ before, so the Windows form part is not so difficult to me. 但是现在,我不知道如何建立C ++程序之间的连接和数据库。互联网上有一些例子,但只有C#或VB才有用。任何人都可以帮我一点,或者给我一个提示,我应该去哪个方向 .. But now, I don't know how to establish the Connection between the C++ program and the database. There are some examples in Internet but only in C# or VB which doesn't help so much. Can anyone help me a bit or give me a hint which direction should i go..我试图将一个C#代码转移到C ++中,如下所示I have tried to transfer one C# code into C++ as following  private:System :: Void button1_Click(System :: Object ^  sender,System: :EventArgs ^  e){   尝试    {     String ^ str =" Data Source =(local); Database = mydata;" ;; >     String ^ query =" select * from data" ;;           ;  SqlConnection ^ con = gcnew SqlConnection(str);     SqlCommand ^ cmd = gcnew SqlCommand(query,con);     con>打开();     DataSet ^ ds = gcnew DataSet();     MessageBox :: Show(" connect with sql server");     con> Close();   }    catch(例外^)    {     MessageBox :: Show(" Error!");   }  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {  try  {   String^ str = "Data Source=(local); Database=mydata;";   String^ query = "select * from data";   SqlConnection^ con = gcnew SqlConnection(str);   SqlCommand^ cmd = gcnew SqlCommand(query, con);   con->Open();   DataSet^ ds = gcnew DataSet();   MessageBox::Show("connect with sql server");   con->Close();  }  catch (Exception^)  {   MessageBox::Show("Error!");  }推荐答案 可能连接字符串不正确。使用下一个 catch 并调查错误:    catch ( 异常 ^ exc)    {   {       MessageBox :: Show( "错误:" + exc-> Message);    }   }   如果添加到Server Explorer面板的连接,则可以在"属性"面板中看到连接字符串。 为了提取数据,您可以使用 cmd-> ExecuteReader 和循环。 或试试这个:    SqlDataAdapter ^ da = gcnew SqlDataAdapter (cmd);   SqlDataAdapter ^ da =gcnewSqlDataAdapter( cmd );      ; da-> Fill(ds);   da->Fill( ds );   数据将添加到数据集中。 这篇关于如何在Visual C ++和MS SQLDB之间建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 03:11