I was told from Networking part of thsi forum to post this here in sql.
Is there a client side approach of sending and receiving data from a sql database without a web service or a server component (servicedcomponent and server app)? How can you simulate a network connection over the internet that could send and receive data from and to database? Can anyone point to a certain document or topic in internet about how to do this?
Thanks.
denpsia
I am not quite sure what you meant by "...without ... server component". Anyway, if you want to connect to sql server, the client tool "osql.exe" can do the job.
Just use osql -? for description about usage.
HTH.
|||Hi MC Wu,
Thanks for reply, MC. What I like to create a internet connection (VPN) programatically. I found some codes about RAS but not for vpn so I modified but its is not working. Can you or anyone help me solved this problem? Advanced thanks.
This inside main class
[code]
public void Dial()
{
if(_Handle!=IntPtr.Zero)
{
RASCONNSTATUS status=new RASCONNSTATUS();
uint res=RasAPI.RasGetConnectStatus(_Handle,status);
if(res==ERROR_INVALID_HANDLE) //res=ERROR_INVALID_HANDLE
_Handle=IntPtr.Zero;
else
return;
}
try
{
this._Params.szPhoneNumber = "<server ip>";
this._Params.szUserName = "<username>";
this._Params.szPassword = "dennispt200508";
RasAPI.RasCheck(RasAPI.RasDial(null,_Phonebook,_Params,1,_DialNotifyDelegate,ref _Handle));
RasAPI.RasConnectionNotification(_Handle,this._DisconnEvent.Handle,RASNOTIFICATION.RASCN_Disconnection);
RasAPI.RasDial(null,@."C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk",_Params,1,_DialNotifyDelegate,ref _Handle);
StartWatch();
if (ConnectionState.IsModemConnected())
this.statusSend("Connected to 203.201.133.64");
else
this.statusSend("Unable to connect.");
}
catch(Exception e)
{
}
}
private void StartWatch()
{
StopWatch();
this._WatchThread=new Thread(new ThreadStart(RunWatch));
this._WatchThread.Start();
}
private void StopWatch()
{
if(this._WatchThread==null)
return;
if(this._WatchThread.IsAlive)
{
this._StopEvent.Set();
this._WatchThread.Join();
}
this._WatchThread=null;
}
private void RunWatch()
{
WaitHandle[] handles=new WaitHandle[]{this._StopEvent,this._DisconnEvent};
int ret;
this._StopEvent.Reset();
this._DisconnEvent.Reset();
while(true)
{
ret=WaitHandle.WaitAny(handles);
if(ret==0)
break;
if(ret==1)
{
this.OnDisconnected();
System.Diagnostics.Debug.WriteLine("Dis connected");
break;
}
}
}
private void OnConnected()
{
if(this.Connected==null)
return;
EventArgs args=new EventArgs();
if(this.SynchronizingObject!=null && this.SynchronizingObject.InvokeRequired)
{
this.SynchronizingObject.Invoke(Connected,new object[]{this,args});
}
else
{
Connected(this,args);
}
}
private void OnDisconnected()
{
if(this.Disconnected==null)
return;
EventArgs args=new EventArgs();
if(this.SynchronizingObject!=null && this.SynchronizingObject.InvokeRequired)
{
this.SynchronizingObject.Invoke(Disconnected,new object[]{this,args});
}
else
{
Disconnected(this,args);
}
}
private void OnDialNotify1(IntPtr hrasconn,uint unMsg,RASCONNSTATE rascs,uint dwError,uint dwExtendedError)
{
if(this.DialNotify1==null)
return;
string msg="";
if(dwError>0)
{
// msg=RasException.Code2RasErrorMessage(dwError);
}
else
{
// msg=RasConnection.GetRasConnStateMessage(rascs);
msg = this.GetRasConnStateMessage(rascs);
}
RasDialNotify1EventArgs args=new RasDialNotify1EventArgs(hrasconn,unMsg,rascs,dwError,dwExtendedError,msg);
if(this.SynchronizingObject!=null && this.SynchronizingObject.InvokeRequired)
{
this.SynchronizingObject.Invoke(DialNotify1,new object[]{this,args});
}
else
{
DialNotify1(this,args);
}
if(args.ConnectionState==RASCONNSTATE.RASCS_Connected)
OnConnected();
}
private string GetRasConnStateMessage(RASCONNSTATE state)
{
string ret="";
if(_Res!=null)
ret=(string)_Res.GetObject(state.ToString().Trim());
return ret;
}
[Browsable(false),Description("get or set SynchronizingObject")]
public ISynchronizeInvoke SynchronizingObject
{
get
{
if(_SynchronizingObject==null)
{
if(this.DesignMode)
{
IDesignerHost dh=this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if(dh!=null)
{
_SynchronizingObject=dh.RootComponent as ISynchronizeInvoke;
}
}
}
return _SynchronizingObject;
}
set
{
_SynchronizingObject=value;
}
}
[/code]
Inside another class
[code]
internal sealed class RasAPI
{
private RasAPI(){}
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
public extern static uint RasDial(
[In]RASDIALEXTENSIONS lpRasDialExtensions,
// pointer to function extensions data
[In]string lpszPhonebook, // pointer to full path and file
// name of phone-book file
[In]RASDIALPARAMS lpRasDialParams,
// pointer to calling parameters data
uint dwNotifierType, // specifies type of RasDial event handler
Delegate lpvNotifier, // specifies a handler for RasDial events
ref IntPtr lphRasConn // pointer to variable to receive
// connection handle
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
public extern static uint RasGetConnectStatus(
IntPtr hrasconn, // handle to RAS connection of interest
[In,Out]RASCONNSTATUS lprasconnstatus
// buffer to receive status data
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
public extern static uint RasGetErrorString(
uint uErrorValue, // error to get string for
StringBuilder lpszErrorString, // buffer to hold error string
[In]int cBufSize // size, in characters, of buffer
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
public extern static uint RasConnectionNotification(
IntPtr hrasconn, // handle to a RAS connection
IntPtr hEvent, // handle to an event object
RASNOTIFICATION dwFlags // type of event to receive notifications for
);
public static void RasCheck(uint errorCode)
{
if(errorCode!=(uint)RasError.SUCCESS)
{
// throw new RasException(errorCode);
StringBuilder sb=new StringBuilder(512);
if(RasAPI.RasGetErrorString(errorCode,sb,sb.Capacity)>0)
throw new Exception("Unknow RAS exception.");
// string ret=sb.ToString();
}
}
}
[/code]
Inside class checking internet connection
[code]
public class ConnectionState
{
private enum ConnectionStateEnum
{
//Local system has a valid connection to the Internet, but it might or might not be currently connected.
ConnectionConfigured = 64,
//Local system uses a local area network to connect to the Internet.
ConnectionLan = 2,
//Local system uses a modem to connect to the Internet.
ConnectionModem = 1,
//No longer used.
ConnectionModemBusy = 8,
//Local system is in offline mode.
ConnectionOffline = 32,
//Local system uses a proxy server to connect to the Internet.
ConnectionProxy = 4,
//Local system has RAS installed.
RasInstalled = 16
}
class Win32
{
[DllImport("Wininet.dll", CharSet=CharSet.Auto)]
public static extern int InternetGetConnectedState(out int Flag, int Reserved);
}
private static int GetConnectionFlag()
{
int Flag;
Win32.InternetGetConnectedState(out Flag,0);
return Flag;
}
public static bool IsModemConnected()
{
return ((GetConnectionFlag() & (int)ConnectionStateEnum.ConnectionModem)==0) ?
false : true;
}
}
[/code]
den2005
No comments:
Post a Comment