EDN Admin
Well-known member
Hi all,
I am new to C# and SQL and learning it fast. I have written the follwing code which is reading some data in a table which has only 4 columns. I want to read the table named "MyTable" and write exactly the same data on to another table called "MyTable_Final".
I am writing this simple code to understand the SQL transactions as well as the use of C# construcotrs and instantiation of objects and types. When I learn this I will introduce more calculations to the code and do modifications to the MyTable and the outcome.
So plese help me and correct the following code.
My code does not have any compile errors. But it seems to lack so many things I think. here is the code.
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Data.SqlClient;<br/>
using System.Data;
namespace GroupCreator<br/>
{
public class GroupCreator<br/>
{<br/>
public IList<Group> GroupList;<br/>
public GroupCreator CallGroup()<br/>
{<br/>
var sample = new GroupCreator();<br/>
SqlCommand myCommand = new SqlCommand();<br/>
myCommand.Connection = new SqlConnection<br/>
("Server=D-701-102-Narada\SQLEXPRESS;"<br/>
+ "Initial Catalog=Numbers;"<br/>
+ "Persist Security Info = true; Integrated Security = SSPI;");<br/>
{<br/>
myCommand.Connection.Open();<br/>
var trns = myCommand.Connection.BeginTransaction(IsolationLevel.Serializable);<br/>
{<br/>
var cmd_all_rows = myCommand.Connection.CreateCommand();<br/>
{<br/>
cmd_all_rows .Transaction = trns;<br/>
cmd_all_rows .CommandText = "SELECT * FROM [dbo].[MyTable] ORDER BY ASC, [J] ASC, [K] ASC";<br/>
var rdr_all = cmd_all_rows .ExecuteReader(CommandBehavior.SingleResult);<br/>
{<br/>
while (rdr_all.Read())<br/>
{<br/>
var number = new Numbers(rdr_all.GetDouble(1), rdr_all.GetDouble(2), rdr_all.GetDouble(3));<br/>
var value = new Value(rdr_all.GetDouble(4));<br/>
sample.GroupList.Add(new Group(number, value));<br/>
}<br/>
rdr_all.Close();<br/>
}<br/>
}<br/>
return sample;<br/>
}<br/>
}<br/>
}
public void DBCreator()<br/>
{<br/>
var connection = new SqlConnection();<br/>
connection.ConnectionString = "Server=D-701-102-Narada\SQLEXPRESS;"<br/>
+ "Initial Catalog=Numbers;"<br/>
+ "Persist Security Info = true; Integrated Security = SSPI;";<br/>
using (var cmd = connection.CreateCommand())// intializing an SQL command object for transactions<br/>
{<br/>
connection.Open();<br/>
cmd.CommandText = "INSERT INTO MyTable_Final (I, J, K,FinalValue) VALUES (@I,@J,@K,@FinalValue) ";
foreach (var group in this.GroupList)<br/>
try<br/>
{
SqlCommand cmdIns = new SqlCommand(cmd.CommandText, connection);<br/>
cmdIns.Parameters.AddWithValue("@I", group.number);<br/>
cmdIns.Parameters.AddWithValue("@J", group.number);<br/>
cmdIns.Parameters.AddWithValue("@K", group.number);<br/>
cmdIns.Parameters.AddWithValue("@FinalValue", group.value);<br/>
cmdIns.ExecuteNonQuery();<br/>
cmdIns.Dispose();<br/>
cmdIns = null;
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.ToString(), ex);<br/>
}<br/>
finally<br/>
{<br/>
}<br/>
connection.Close();<br/>
}<br/>
}<br/>
public class Numbers<br/>
{<br/>
double I;<br/>
double J;<br/>
double K;
public Numbers(double i, double j, double k)<br/>
{<br/>
I = i;<br/>
J = j;<br/>
K = k;<br/>
}<br/>
}
public class Value<br/>
{<br/>
double FinalValue;<br/>
public Value(double finalvalue)<br/>
{<br/>
FinalValue = finalvalue;<br/>
}<br/>
}<br/>
public class Group<br/>
{<br/>
public Numbers number;<br/>
public Value value;<br/>
public Group(Numbers x, Value y)<br/>
{<br/>
number = x;<br/>
value = y;<br/>
}
}<br/>
}<br/>
}
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;
namespace GroupCreator<br/>
{<br/>
class Program<br/>
{<br/>
static void Main(string[] args)<br/>
{<br/>
GroupCreator creator = new GroupCreator();<br/>
creator.CallGroup();<br/>
creator.DBCreator();<br/>
}<br/>
}<br/>
}<br/>
There seems to be a few problems in this code. For example when I run this I get an error that states "Object reference not set to an instance of an object" on the foreach statement inside the DBCreator method.
And also, although I expect to write the I,J,K values seperately they do not appear in the inteli-sense menu down. For example I need the statement mailto: "cmdIns.Parameters.AddWithValue("@I ", group.number);" to be like mailto: "cmdIns.Parameters.AddWithValue("@I ",
group.number.i); But I do not get the number.i in the intelisense menu. Is there a problem with the way I initiated the constructor for the Numbers class??? Please help me to understand the constructor as well.
I am sure there are serious errors may be in the way I have declared variables, constructors, etc in this code even though there are no compile errors.
Can you please have a look at the code and try to correct this. If you can correct this that will be great for my future learning and I can build up my C# knowledge and improve from there.
All your tips and hints and lessons and corrections are higly appreciated.
Many thanks in advance!!
Please help me!
Narada.
<br/>
<br/>
<br/>
View the full article
I am new to C# and SQL and learning it fast. I have written the follwing code which is reading some data in a table which has only 4 columns. I want to read the table named "MyTable" and write exactly the same data on to another table called "MyTable_Final".
I am writing this simple code to understand the SQL transactions as well as the use of C# construcotrs and instantiation of objects and types. When I learn this I will introduce more calculations to the code and do modifications to the MyTable and the outcome.
So plese help me and correct the following code.
My code does not have any compile errors. But it seems to lack so many things I think. here is the code.
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Data.SqlClient;<br/>
using System.Data;
namespace GroupCreator<br/>
{
public class GroupCreator<br/>
{<br/>
public IList<Group> GroupList;<br/>
public GroupCreator CallGroup()<br/>
{<br/>
var sample = new GroupCreator();<br/>
SqlCommand myCommand = new SqlCommand();<br/>
myCommand.Connection = new SqlConnection<br/>
("Server=D-701-102-Narada\SQLEXPRESS;"<br/>
+ "Initial Catalog=Numbers;"<br/>
+ "Persist Security Info = true; Integrated Security = SSPI;");<br/>
{<br/>
myCommand.Connection.Open();<br/>
var trns = myCommand.Connection.BeginTransaction(IsolationLevel.Serializable);<br/>
{<br/>
var cmd_all_rows = myCommand.Connection.CreateCommand();<br/>
{<br/>
cmd_all_rows .Transaction = trns;<br/>
cmd_all_rows .CommandText = "SELECT * FROM [dbo].[MyTable] ORDER BY ASC, [J] ASC, [K] ASC";<br/>
var rdr_all = cmd_all_rows .ExecuteReader(CommandBehavior.SingleResult);<br/>
{<br/>
while (rdr_all.Read())<br/>
{<br/>
var number = new Numbers(rdr_all.GetDouble(1), rdr_all.GetDouble(2), rdr_all.GetDouble(3));<br/>
var value = new Value(rdr_all.GetDouble(4));<br/>
sample.GroupList.Add(new Group(number, value));<br/>
}<br/>
rdr_all.Close();<br/>
}<br/>
}<br/>
return sample;<br/>
}<br/>
}<br/>
}
public void DBCreator()<br/>
{<br/>
var connection = new SqlConnection();<br/>
connection.ConnectionString = "Server=D-701-102-Narada\SQLEXPRESS;"<br/>
+ "Initial Catalog=Numbers;"<br/>
+ "Persist Security Info = true; Integrated Security = SSPI;";<br/>
using (var cmd = connection.CreateCommand())// intializing an SQL command object for transactions<br/>
{<br/>
connection.Open();<br/>
cmd.CommandText = "INSERT INTO MyTable_Final (I, J, K,FinalValue) VALUES (@I,@J,@K,@FinalValue) ";
foreach (var group in this.GroupList)<br/>
try<br/>
{
SqlCommand cmdIns = new SqlCommand(cmd.CommandText, connection);<br/>
cmdIns.Parameters.AddWithValue("@I", group.number);<br/>
cmdIns.Parameters.AddWithValue("@J", group.number);<br/>
cmdIns.Parameters.AddWithValue("@K", group.number);<br/>
cmdIns.Parameters.AddWithValue("@FinalValue", group.value);<br/>
cmdIns.ExecuteNonQuery();<br/>
cmdIns.Dispose();<br/>
cmdIns = null;
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.ToString(), ex);<br/>
}<br/>
finally<br/>
{<br/>
}<br/>
connection.Close();<br/>
}<br/>
}<br/>
public class Numbers<br/>
{<br/>
double I;<br/>
double J;<br/>
double K;
public Numbers(double i, double j, double k)<br/>
{<br/>
I = i;<br/>
J = j;<br/>
K = k;<br/>
}<br/>
}
public class Value<br/>
{<br/>
double FinalValue;<br/>
public Value(double finalvalue)<br/>
{<br/>
FinalValue = finalvalue;<br/>
}<br/>
}<br/>
public class Group<br/>
{<br/>
public Numbers number;<br/>
public Value value;<br/>
public Group(Numbers x, Value y)<br/>
{<br/>
number = x;<br/>
value = y;<br/>
}
}<br/>
}<br/>
}
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;
namespace GroupCreator<br/>
{<br/>
class Program<br/>
{<br/>
static void Main(string[] args)<br/>
{<br/>
GroupCreator creator = new GroupCreator();<br/>
creator.CallGroup();<br/>
creator.DBCreator();<br/>
}<br/>
}<br/>
}<br/>
There seems to be a few problems in this code. For example when I run this I get an error that states "Object reference not set to an instance of an object" on the foreach statement inside the DBCreator method.
And also, although I expect to write the I,J,K values seperately they do not appear in the inteli-sense menu down. For example I need the statement mailto: "cmdIns.Parameters.AddWithValue("@I ", group.number);" to be like mailto: "cmdIns.Parameters.AddWithValue("@I ",
group.number.i); But I do not get the number.i in the intelisense menu. Is there a problem with the way I initiated the constructor for the Numbers class??? Please help me to understand the constructor as well.
I am sure there are serious errors may be in the way I have declared variables, constructors, etc in this code even though there are no compile errors.
Can you please have a look at the code and try to correct this. If you can correct this that will be great for my future learning and I can build up my C# knowledge and improve from there.
All your tips and hints and lessons and corrections are higly appreciated.
Many thanks in advance!!
Please help me!
Narada.
<br/>
<br/>
<br/>
View the full article