Caute, mam jeden problem, hodil som tento kod do VC++ a vypisuje mi to chybu:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326).
Pri zakladani databazy som postupoval podla:
http://www.inet.sk/clanok/4296/programu ... sql-server
A chybu som sa snazil odstranit podla:
http://blog.sqlauthority.com/2009/05/21 ... ver-error/
Neviete v com je problem, nebude chyba v tomto:
ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";
Ale vsetko zadavam podla mna spravne, tak neviem, ci tam nie je nejaka ina chyba.
Dakujem za rady.
Kód:
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
SqlConnection *cn = new SqlConnection();
DataSet *CustomersDataSet = new DataSet();
SqlDataAdapter *da;
SqlCommandBuilder *cmdBuilder;
//Set the connection string of the SqlConnection object to connect
//to the SQL Server database in which you created the sample
//table in Section 1.0
cn->ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";
cn->Open();
//Initialize the SqlDataAdapter object by specifying a Select command
//that retrieves data from the sample table
da = new SqlDataAdapter("select * from CustTest order by CustId", cn);
//Initialize the SqlCommandBuilder object to automatically generate and initialize
//the UpdateCommand, InsertCommand and DeleteCommand properties of the SqlDataAdapter
cmdBuilder = new SqlCommandBuilder(da);
//Populate the DataSet by executing the Fill method of the SqlDataAdapter
da->Fill(CustomersDataSet, "Customers");
//Display the Update, Insert and Delete commands that were automatically generated
//by the SqlCommandBuilder object
Console::WriteLine("Update command Generated by the Command Builder : ");
Console::WriteLine("==================================================");
Console::WriteLine(cmdBuilder->GetUpdateCommand()->CommandText);
Console::WriteLine(" ");
Console::WriteLine("Insert command Generated by the Command Builder : ");
Console::WriteLine("==================================================");
Console::WriteLine(cmdBuilder->GetInsertCommand()->CommandText);
Console::WriteLine(" ");
Console::WriteLine("Delete command Generated by the Command Builder : ");
Console::WriteLine("==================================================");
Console::WriteLine(cmdBuilder->GetDeleteCommand()->CommandText);
Console::WriteLine(" ");
//Write out the value in the CustName field before updating the data using the DataSet
DataRow *rowCust = CustomersDataSet->Tables->Item["Customers"]->Rows->Item[0];
Console::WriteLine("Customer Name before Update : {0} ", rowCust->Item["CustName"]);
//Modify the value of the CustName field
String *newStrVal = new String("Jack");
rowCust->set_Item("CustName", newStrVal);
//Modify the value of the CustName field again
String *newStrVal2 = new String("Jack2");
rowCust->set_Item("CustName", newStrVal2);
//Post the data modification to the database
da->Update(CustomersDataSet, "Customers");
Console::WriteLine("Customer Name after Update : {0} ", rowCust->Item["CustName"]);
//Close the database connection
cn->Close();
//Pause
Console::ReadLine();
return 0;
}