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