
This shows simple examples of common coding activities. It is a work in progress
and is mainly for my own use, but it may hold the information you want.
Many are the issues with Ado.Net, and I've not got to the bottom of it yet, but here is an Ado.Net crib sheet all of its own. And also here, the simple high points - all of which are illustrated in the working 'TheMain.cs' file.
| SqlConnection and connection string | |
SqlConnection dbcon = new SqlConnection(); |
|
| Connecting to SqlServer with a named instance (forget the correct terminology) | |
| Basically the Server=(local) may not work with a named instance or with DNS issues. For named instances use the IP address\instance_name. eg I have a sqlserver instance called JPGPRIV, so the server element of the connection string is: Server=192.168.0.5\JPGPRIV |
|
| Typed data set Fill | |
String query = "SELECT id, description from CONSource"; |
|
| Inserting into a typed data set | |
for (int cnt=0; cnt<10; cnt++) { |
|
| Commiting a data set back to the data source | |
da = new SqlDataAdapter(query, dbcon); |
|
| Commiting a dataset and retrieving the real auto inc numbers | |
| In the data source set seed as 0, increment as -1, and readonly to false for the autoinc column. This allows the local dataset to assign decreasing negative keys, while the server assigns increasing positive keys. da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); // sqlserver 2000 version
SqlCommand id_cmd = new SqlCommand("select scope_identity() as id", con);
new_id = id_cmd.ExecuteScalar();
if (new_id== System.DBNull.Value) {
// sqlserver earlier version
id_cmd = new SqlCommand("select @@IDENTITY", con);
new_id = id_cmd.ExecuteScalar();
if (new_id== System.DBNull.Value) {
throw new Exception("Why doesn't it work??!!");
}
}
// Retrieve the identity value and store it in the id column.
args.Row["id"] = new_id;
}
} // end of onRowUpdated
|
|
| Untyped data set definition | |
DataTable tab = dset2.Tables["CONSource"]; |
|
| Updating untyped data set | |
for (int cnt=0; cnt<3; cnt++) { |
|
| Assigning nulls | |
anyRow["description"] = DBNull.Value; |
|
Testing nulls |
|
if (new_id== System.DBNull.Value) { |
|
| Transaction - multiple datasets/adaptors | |
con.Open(); |
|
| GUID's | |
| Now Guid's are nasty, for various reasons, but if you go for them then use the System.Guid class member NewGuid(). Guid val = Guid.NewGuid(); |
|
| SqlCommand and SqlDataReader example | |
try { |
General comment. Attempts to write a threaded web proxy failed dismally because browser socket close events are not detected by the TcpClient and NetworkStream classes within the proxy. The solution is to use Socket classes instead. i.e. use TcpClient for one off comms, not looping comms which you may want to terminate once the read has started.
| Listen on a port: | Link |
TCPListener server = new TCPListener( 23 ); server.Start(); Socket accept = server.Accept(); |
|
| Listen on a port take II | Link |
try { |
|
| Connect to a port with sockets: | |
IPHostEntry ipHostInfo = Dns.Resolve(host); |
|
| Connect to a port: | Link |
TCPClient telnet = new TCPClient( "telnet.host.com", 23 ); Stream telnetStream = telnet.GetStream(); StreamWriter output = new StreamWriter( telnetStream ); StreamReader input = new StreamReader( telnetStream ); |
|
| Request a web page | Link |
WebRequest request = WebRequestFactory.Create( = "http://to.post.to.com" ); Stream input = request.GetResponse().GetResponseStream(); ... read stuff from input ... input.Close(); |
|
| Link | |
| No code here, but a .Net horror. All the socket exception thrown include a useless error number which is thrown by the underlying winsock libs. A quick search found a german firewall site which allowed me to work out what the errors actually were. Follow the link for the information - you don't have to read german. |
|
| Create a text file | Link |
| StreamWriter sw File.CreateText( "brand-new-file.txt" ); | |
public static void getDiskSize(String drive,
out ulong o_size, out ulong o_used, out ulong o_avail) {
// Check the drive exists
if (!Directory.Exists(drive+@":\"))
throw new DirectoryNotFoundException("No drive info");
ManagementObject disk = new
ManagementObject("Win32_LogicalDisk.DeviceID=\""+drive+":\"");
UInt64 freespace = (UInt64)disk.Properties["FreeSpace"].Value ; UInt64 size = (UInt64)disk.Properties["Size"].Value ; Console.WriteLine("Harddisk size: "+ size);
Console.WriteLine("Bytes used: "+ (size - freespace));
Console.WriteLine("Bytes availlable: "+ freespace);
o_size = size;
o_used= ((size - freespace));
o_avail = freespace;
} |
|
| Disk Names | |
| String [] drives = Directory.GetLogicalDrives(); | |
| How to get the current time | |
DateTime.Now; |
|
public enum RESPONSE_CODES : int {
OK = 200,
BadRequest = 400,
InternalServerError = 500,
}; |
|
All that single thread appartment and multi thread appartment stuff is MS rubbish based upon VB history. Ignore it all, and just use nice, normal java type threads.