|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Connect to SQL Server 2005 Using Visual Studio C++ 2005 ?I hope I am in the correct group to ask such a question, anyway: I am new to Visual Studio C++ (2005) and SQL Server 2005 as I have recently moved across from using Borland C++, so VS has been pretty much a steep learning curve for me. I have created a database through SQL Server called dbTest.mdf, I did this from SQLCMD (CREATE DATABASE dbTest). I now want to connect to the database using Visual Studio C++ 2005 by writing a simple app. Obviously the code included below won't work - well I can't get it to work, so please have a look at it and tell me where I'm going wrong..... Best Regards Pete #include "stdafx.h" using namespace System; using namespace System::Data; using namespace System::Data::Sql; using namespace System::Data::SqlClient; int main(array<System::String ^> ^args) { SqlConnection conn; conn = new SqlConnection("Data Source=PETER-VTR1000\SQLEXPRESS;Initial Catalog=dbTest;Integrated Security=True"); return 0; } -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Show quote
Hide quote
"Pete Moscatt" <peter.mosc***@gmail.com> wrote in message Sure. This is C++/CLI.news:448a5f1b$0$29247$b9f67a60@news.newsdemon.com... > Hi All, > > I hope I am in the correct group to ask such a question, anyway: > > I am new to Visual Studio C++ (2005) and SQL Server 2005 as I have > recently moved across from using Borland C++, so VS has been pretty much a > steep learning curve for me. > > I have created a database through SQL Server called dbTest.mdf, I did this > from SQLCMD (CREATE DATABASE dbTest). > > I now want to connect to the database using Visual Studio C++ 2005 by > writing a simple app. > > Obviously the code included below won't work - well I can't get it to > work, so please have a look at it and tell me where I'm going wrong..... > Your other option to access SqlServer form C++ is OleDB. But I like this much better since you get access to the whole .NET framework, and you can use garbage-collected types. Also there are tons of examples of how to use SqlServer through .NET in C#. Once you master the syntax differences between C# and C++/CLI you can easilly use them. Anyway here's your code rewritten a bit. David #include "stdafx.h" using namespace System; using namespace System::Data; using namespace System::Data::Sql; using namespace System::Data::SqlClient; void Run() { /* SqlConnection implements IDisposable and must be cleaned up after it's used. But declaring as a local variable intead of a reference using gcnew the compiler will insert the cleanup code at the end of this scope. */ SqlConnection conn("Data Source=(local);Initial Catalog=test;Integrated Security=True"); conn.Open(); /* SqlCommand doesn't really need cleanup, so we can create that using gcnew and access it through a handle. */ SqlCommand^ cmd = gcnew SqlCommand("select top 10 Name from sys.objects",%conn); /* SqlDataReader also needs cleanup, but it is returned from the SqlCommand as a handle so we can't use automatic storage for it. So a try/finally block guarantees cleanup. */ SqlDataReader^ rdr = cmd->ExecuteReader(); try { while (rdr->Read()) { System::Console::WriteLine(rdr[0]); } } finally { rdr->Close(); } } int main(array<System::String ^> ^args) { try { Run(); return 0; } catch (Exception^ ex) { Console::WriteLine(ex); return 1; } } David Browne wrote:
Show quoteHide quote > "Pete Moscatt" <peter.mosc***@gmail.com> wrote in message G'Dave,> news:448a5f1b$0$29247$b9f67a60@news.newsdemon.com... >> Hi All, >> >> I hope I am in the correct group to ask such a question, anyway: >> >> I am new to Visual Studio C++ (2005) and SQL Server 2005 as I have >> recently moved across from using Borland C++, so VS has been pretty much a >> steep learning curve for me. >> >> I have created a database through SQL Server called dbTest.mdf, I did this >> from SQLCMD (CREATE DATABASE dbTest). >> >> I now want to connect to the database using Visual Studio C++ 2005 by >> writing a simple app. >> >> Obviously the code included below won't work - well I can't get it to >> work, so please have a look at it and tell me where I'm going wrong..... >> > > Sure. This is C++/CLI. > > Your other option to access SqlServer form C++ is OleDB. But I like this > much better since you get access to the whole .NET framework, and you can > use garbage-collected types. > > Also there are tons of examples of how to use SqlServer through .NET in C#. > Once you master the syntax differences between C# and C++/CLI you can > easilly use them. > > Anyway here's your code rewritten a bit. > > David > > > > #include "stdafx.h" > > using namespace System; > using namespace System::Data; > using namespace System::Data::Sql; > using namespace System::Data::SqlClient; > > void Run() > { > /* > SqlConnection implements IDisposable and must be cleaned up after it's > used. > But declaring as a local variable intead of a reference using gcnew > the compiler > will insert the cleanup code at the end of this scope. > */ > SqlConnection conn("Data Source=(local);Initial Catalog=test;Integrated > Security=True"); > conn.Open(); > > /* > SqlCommand doesn't really need cleanup, so we can create that using > gcnew and access > it through a handle. > */ > SqlCommand^ cmd = gcnew SqlCommand("select top 10 Name from > sys.objects",%conn); > > /* > SqlDataReader also needs cleanup, but it is returned from the > SqlCommand as a handle > so we can't use automatic storage for it. So a try/finally block > guarantees cleanup. > */ > SqlDataReader^ rdr = cmd->ExecuteReader(); > try > { > while (rdr->Read()) > { > System::Console::WriteLine(rdr[0]); > } > } > finally > { > rdr->Close(); > } > > } > > int main(array<System::String ^> ^args) > { > try > { > Run(); > return 0; > } > catch (Exception^ ex) > { > Console::WriteLine(ex); > return 1; > } > } > > > Thanks for the code - at least it now compiles. When I run the EXE I am now faced with connection errors so now assume that I need to pay more attention on how I installed SQL Server. So, back to the books to clear that up - I can use SQLCMD so I guess I can't be too far away on getting it right. Currently I have the server connecting with TCP (port 1403) and I see I could also used: Named Pipes TCP VIA What is considered the better way to connect ? I had a look at the configuration of the Server and found to be 'local service' Would I be better of using: local system or network service or local service (current) Cheers Pete -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Show quote
Hide quote
"Pete Moscatt" <peter.mosc***@gmail.com> wrote in message You probably need an instance name in the "Data Source".news:448b95c4$0$30209$b9f67a60@news.newsdemon.com... > David Browne wrote: >> "Pete Moscatt" <peter.mosc***@gmail.com> wrote in message >> news:448a5f1b$0$29247$b9f67a60@news.newsdemon.com... >>> Hi All, >>> >>> I hope I am in the correct group to ask such a question, anyway: >>> >>> I am new to Visual Studio C++ (2005) and SQL Server 2005 as I have >>> recently moved across from using Borland C++, so VS has been pretty much >>> a steep learning curve for me. >>> >>> I have created a database through SQL Server called dbTest.mdf, I did >>> this from SQLCMD (CREATE DATABASE dbTest). >>> >>> I now want to connect to the database using Visual Studio C++ 2005 by >>> writing a simple app. >>> >>> Obviously the code included below won't work - well I can't get it to >>> work, so please have a look at it and tell me where I'm going wrong..... >>> >> >> Sure. This is C++/CLI. >> >> Your other option to access SqlServer form C++ is OleDB. But I like this >> much better since you get access to the whole .NET framework, and you can >> use garbage-collected types. >> >> Also there are tons of examples of how to use SqlServer through .NET in >> C#. Once you master the syntax differences between C# and C++/CLI you can >> easilly use them. >> >> Anyway here's your code rewritten a bit. >> >> David >> >> >> >> #include "stdafx.h" >> >> using namespace System; >> using namespace System::Data; >> using namespace System::Data::Sql; >> using namespace System::Data::SqlClient; >> >> void Run() >> { >> /* >> SqlConnection implements IDisposable and must be cleaned up after >> it's used. >> But declaring as a local variable intead of a reference using gcnew >> the compiler >> will insert the cleanup code at the end of this scope. >> */ >> SqlConnection conn("Data Source=(local);Initial >> Catalog=test;Integrated Security=True"); >> conn.Open(); >> >> /* >> SqlCommand doesn't really need cleanup, so we can create that using >> gcnew and access >> it through a handle. >> */ >> SqlCommand^ cmd = gcnew SqlCommand("select top 10 Name from >> sys.objects",%conn); >> >> /* >> SqlDataReader also needs cleanup, but it is returned from the >> SqlCommand as a handle >> so we can't use automatic storage for it. So a try/finally block >> guarantees cleanup. >> */ >> SqlDataReader^ rdr = cmd->ExecuteReader(); >> try >> { >> while (rdr->Read()) >> { >> System::Console::WriteLine(rdr[0]); >> } >> } >> finally >> { >> rdr->Close(); >> } >> >> } >> >> int main(array<System::String ^> ^args) >> { >> try >> { >> Run(); >> return 0; >> } >> catch (Exception^ ex) >> { >> Console::WriteLine(ex); >> return 1; >> } >> } >> >> >> > > G'Dave, > > Thanks for the code - at least it now compiles. > When I run the EXE I am now faced with connection errors so now assume > that I need to pay more attention on how I installed SQL Server. > > So, back to the books to clear that up - I can use SQLCMD so I guess I > can't be too far away on getting it right. > > Currently I have the server connecting with TCP (port 1403) and I see I > could also used: > SqlConnection conn("Data Source=(local)\SQLExpress;Initial Catalog=test;Integrated Security=True"); conn.Open(); And "Initial Catalog" is the database name. I would generally use TCP/IP (remote) and Shared Memory (local). > Named Pipes As for what account you use to run SQL Server, it really depends on your > TCP > VIA > > What is considered the better way to connect ? > > I had a look at the configuration of the Server and found to be 'local > service' > security setup. Usually either Local Service or Network Service is appropriate. But sometimes you will want to use a Domain account. David On Sun, 11 Jun 2006 11:10:50 -0500, "David Browne" <davidbaxterbrowne
no potted m***@hotmail.com> wrote: Show quoteHide quote > Thanks Dave, it all a good thing at the moment.>"Pete Moscatt" <peter.mosc***@gmail.com> wrote in message >news:448b95c4$0$30209$b9f67a60@news.newsdemon.com... >> David Browne wrote: >>> "Pete Moscatt" <peter.mosc***@gmail.com> wrote in message >>> news:448a5f1b$0$29247$b9f67a60@news.newsdemon.com... >>>> Hi All, >>>> >>>> I hope I am in the correct group to ask such a question, anyway: >>>> >>>> I am new to Visual Studio C++ (2005) and SQL Server 2005 as I have >>>> recently moved across from using Borland C++, so VS has been pretty much >>>> a steep learning curve for me. >>>> >>>> I have created a database through SQL Server called dbTest.mdf, I did >>>> this from SQLCMD (CREATE DATABASE dbTest). >>>> >>>> I now want to connect to the database using Visual Studio C++ 2005 by >>>> writing a simple app. >>>> >>>> Obviously the code included below won't work - well I can't get it to >>>> work, so please have a look at it and tell me where I'm going wrong..... >>>> >>> >>> Sure. This is C++/CLI. >>> >>> Your other option to access SqlServer form C++ is OleDB. But I like this >>> much better since you get access to the whole .NET framework, and you can >>> use garbage-collected types. >>> >>> Also there are tons of examples of how to use SqlServer through .NET in >>> C#. Once you master the syntax differences between C# and C++/CLI you can >>> easilly use them. >>> >>> Anyway here's your code rewritten a bit. >>> >>> David >>> >>> >>> >>> #include "stdafx.h" >>> >>> using namespace System; >>> using namespace System::Data; >>> using namespace System::Data::Sql; >>> using namespace System::Data::SqlClient; >>> >>> void Run() >>> { >>> /* >>> SqlConnection implements IDisposable and must be cleaned up after >>> it's used. >>> But declaring as a local variable intead of a reference using gcnew >>> the compiler >>> will insert the cleanup code at the end of this scope. >>> */ >>> SqlConnection conn("Data Source=(local);Initial >>> Catalog=test;Integrated Security=True"); >>> conn.Open(); >>> >>> /* >>> SqlCommand doesn't really need cleanup, so we can create that using >>> gcnew and access >>> it through a handle. >>> */ >>> SqlCommand^ cmd = gcnew SqlCommand("select top 10 Name from >>> sys.objects",%conn); >>> >>> /* >>> SqlDataReader also needs cleanup, but it is returned from the >>> SqlCommand as a handle >>> so we can't use automatic storage for it. So a try/finally block >>> guarantees cleanup. >>> */ >>> SqlDataReader^ rdr = cmd->ExecuteReader(); >>> try >>> { >>> while (rdr->Read()) >>> { >>> System::Console::WriteLine(rdr[0]); >>> } >>> } >>> finally >>> { >>> rdr->Close(); >>> } >>> >>> } >>> >>> int main(array<System::String ^> ^args) >>> { >>> try >>> { >>> Run(); >>> return 0; >>> } >>> catch (Exception^ ex) >>> { >>> Console::WriteLine(ex); >>> return 1; >>> } >>> } >>> >>> >>> >> >> G'Dave, >> >> Thanks for the code - at least it now compiles. >> When I run the EXE I am now faced with connection errors so now assume >> that I need to pay more attention on how I installed SQL Server. >> >> So, back to the books to clear that up - I can use SQLCMD so I guess I >> can't be too far away on getting it right. >> >> Currently I have the server connecting with TCP (port 1403) and I see I >> could also used: >> > > > >You probably need an instance name in the "Data Source". > > SqlConnection conn("Data Source=(local)\SQLExpress;Initial >Catalog=test;Integrated Security=True"); > conn.Open(); > >And "Initial Catalog" is the database name. > >I would generally use TCP/IP (remote) and Shared Memory (local). > > > >> Named Pipes >> TCP >> VIA >> >> What is considered the better way to connect ? >> >> I had a look at the configuration of the Server and found to be 'local >> service' >> > >As for what account you use to run SQL Server, it really depends on your >security setup. Usually either Local Service or Network Service is >appropriate. But sometimes you will want to use a Domain account. > > >David > You have me enough info for me to let loose and work things out myself. Again, thanks for all the assistance you have given me on this issue. Pete -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Other interesting topics
How to use getdate() and user_name() functions with Windows NT login
Cursor doesn't loop properly??? create function Timestamp Question Add auto number field to view how to convert varchar into datetime field SQL-92 varchar maxlength save results to multiple files Is it possible to make a HTTP Post in TSQL? (SQL Server 2000) Datetime problem |
|||||||||||||||||||||||