473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reference for database handling in windows forms using C#.Net

Hello,
Can anyone please give me any referencese or links related to
database handling in windows forms using C#.Net.

Mar 17 '06 #1
2 3753
Simply, you can do like this

using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

public static string Transaction(string sql, string conn)
{
try
{
SqlConnection myConnection = new SqlConnection(conn);
SqlCommand myCommand = myConnection.CreateCommand();

myCommand.CommandText = sql;
myConnection.Open();
myCommand.ExecuteNonQuery();

myConnection.Close();

return "";
}
catch(Exception ex)
{
string msg = @"\n" + sql + @"\n"+ex;
return msg;
}
}

Mar 17 '06 #2
bu******@gmail.com <bu******@gmail.com> wrote:
Simply, you can do like this

using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

public static string Transaction(string sql, string conn)
{
try
{
SqlConnection myConnection = new SqlConnection(conn);
SqlCommand myCommand = myConnection.CreateCommand();

myCommand.CommandText = sql;
myConnection.Open();
myCommand.ExecuteNonQuery();

myConnection.Close();

return "";
}
catch(Exception ex)
{
string msg = @"\n" + sql + @"\n"+ex;
return msg;
}
}


Well, you can - but you shouldn't:

1) Your connection and command aren't being closed when an exception is
thrown - use a couple of "using" blocks instead.

2) Your method of indicating errors is very odd. Why not just let the
exception propagate? Using a string instead of an exception is a bad
idea to start with, and why do you want a literal \n between the two
pieces of information, instead of an actual line feed?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Hunter | last post by:
I've recently had a nasty problem with the "Invalid reference to the property Form" error in subforms - nasty because it doesn't seem to consistently happen to all forms which contain the same...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
4
by: Cybertof | last post by:
Hello, What is the difference between : - adding a new reference to a namespace within the SolutionExplorer (right click, Add Reference...) - adding a new reference with the 'using' keyword in...
3
by: Poewood | last post by:
Okay here are four classes for a pocket pc program: Input, fpositional, ComboBoxArray and TextBoxArray. The "input" class is the form. I use the fpositional class to handle most of the functions...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
0
by: Patrick | last post by:
This is a C# post. I'm using VB.NET to create an add-in for the Google Sidebar, and have implemented the OnDetailsView method. NOTE : I've come across this same problem (error message) even in...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.