Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get connection string and provider name using a function like this:

C#
MyDriver* GetDSNAndProvider(const String* sName,String *sDSN,String *sProvider)
{
    if(1)
    {
        sDSN="Data Source=(local);Initial Catalog=labops;User Id=labops;      Password=99w1ldf1re;";
        sProvider="System.Data.SqlClient";
        MyDriver *myIface = new SQlClientDriver();
       return myIface;
    }
    else
    {
        return NULL;
    }
}



and this is my Main()

C#
int _tmain(int argc, _TCHAR* argv[])
{
	

	String *sDQN,*sProvider;
	try
    {
	MyDriver *MDriverIface=GetDSNAndProvider("sqlServer",sDQN,sProvider);
        Console::Write(String::Concat("Provider Name ",   sProvider));
    }
}


I am getting the provider name as null, How we can do this call by reference in c++.net. I am compiling this code using "Common Language Runtime Support, Old Syntax (/clr:oldSyntax)".
Posted
Updated 25-Nov-14 17:54pm
v5
Comments
Tomas Takac 25-Nov-14 9:17am    
Your method expects provider name "sql server" but you are passing it as "sqlServer".
Member 11168418 25-Nov-14 23:37pm    
I have tried with if condition true at all time, replaced with if(1){}, still return nothing.
Member 11168418 25-Nov-14 23:41pm    
I tried like this also:
if(1)
{
sDSN="Data Source=(local);Initial Catalog=labops;User Id=labops;Password=99w1ldf1re;";
sProvider="System.Data.SqlClient";
MyDriver *myIface = new SQlClientDriver();
return myIface;

}
else
{
return NULL;
}
still not working.

This looks like C++/CLI.

1. Your gc objects need hats eg. "^".
2. you're passing pointer values which are not the same as references.
3. you fail to return the new pointer to the oracle interface.

I created a mock of the two MyDriver implementations to verify it works. I'm assuming a C++ class and not a ref class. I noticed the typo in the SQL server name.

C++
#include "stdafx.h"
#include <stddef.h>
#include <string.h>

using namespace System;

struct MyDriver
{
	int common;
public:
	virtual const char *whoami() const = 0;
};

struct SQlClientDriver : MyDriver
{
	int sql;
public:
	const char *whoami() const { return "SQL"; }
};

struct OracleClientDriver : MyDriver
{
	int oracle;
public:
	const char *whoami() const { return "Oracle"; }
};

MyDriver* GetDSNAndProvider(String^ sName, String ^ &sDSN, String ^ &sProvider)
{
    if(sName == "sqlServer")
    {
		sDSN="Data Source=(local);Initial Catalog=myDb;User Id=user;Password=pw;";
		sProvider="System.Data.SqlClient";
		return new SQlClientDriver();//class is defined there
	}
	else if (sName == "Oracle")
	{
		sDSN="Data Source=(local);Initial Catalog=myOracleDB;User Id=user;Password=pw;";
		sProvider="System.Data.SqlClient";
		return new OracleClientDriver();//class is defined there
	}
	return NULL;
}

int main(array<system::string xmlns:system="#unknown"> ^args)
{
    Console::WriteLine(L"Starting ...");
    String^ sDQN, ^sProvider;

    MyDriver *MDriverIface = GetDSNAndProvider("sqlServer",sDQN,sProvider);
    Console::WriteLine("Provider Name {0}", sProvider);
    if (MDriverIface != NULL)
    {
        const char *whoami = MDriverIface->whoami();
        IntPtr pass = (IntPtr)const_cast<char>(whoami);
        String^ marshal = System::Runtime::InteropServices::Marshal::PtrToStringAnsi(pass, strlen(whoami));
        Console::WriteLine(marshal);
    }

    Console::WriteLine(L"Done.");
    Console::ReadLine();
    return 0;
}
</char></system::string></string.h></stddef.h>


I purposely omitted the try/catch to make errors more obvious.
 
Share this answer
 
v2
Comments
Member 11168418 25-Nov-14 23:34pm    
If we use String^ sDQN, ^sProvider; instead of String *sDQN, *sProvider;
an error occured like:

Error 1 error C3191: '^' : incompatible with '/clr:oldSyntax'
Member 11168418 26-Nov-14 1:28am    
Hi,
It worked fine, compiled with /clr option.
Thanks
nv3 26-Nov-14 7:51am    
My 5.
why dont you check and handle that case:

C#
if( sProvider == null)
{
  sProvider = "my fallback";
}


And as Tomas wrote: Your method expects provider name "sql server" but you are passing it as "sqlServer".!!! :-O
 
Share this answer
 
Comments
Member 11168418 25-Nov-14 23:40pm    
i just tried without those conditions like
if(1)
{
sDSN="Data Source=(local);Initial Catalog=labops;User Id=labops;Password=99w1ldf1re;";
sProvider="System.Data.SqlClient";
MyDriver *myIface = new SQlClientDriver();
return myIface;

}
else
{
return NULL;
}

inside GetDSNAndProvider() function, still return nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900