W
Wing
Hi all,
I am writing the C# code function that take 2 parameters and access
the data in the MS SQL database and return a SqlDataReader object. In
my funtion, a SQL connection is established and open the connnection
before reading the data. Everything seem to be fine, but once I add the
close() function to close the sql connection, error come out. So I
leave out the close() function in my function. My code is showing below
-------------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string found = "c553a15f-15bd-4081-bd5d-89cfa2cc0505";
int photoId = 3;
SqlDataReader answer = ReadQuantity(found, photoId);
while(answer.Read())
{
int photoQuantity = Convert.ToInt16(answer["Quantity"]);
Console.WriteLine("\t{0}", photoQuantity);
}
}
static SqlDataReader ReadQuantity(string cartId, int photoId)
{
SqlConnection thisConnection = new SqlConnection
(@"Data Source=(local);Integrated Security=SSPI;" +
"Initial Catalog=PhotoDB");
thisConnection.Open();
SqlCommand ReadQuantity = thisConnection.CreateCommand();
ReadQuantity.CommandText = "SELECT Quantity FROM ShoppingCart WHERE
CartID = '"+cartId+"' AND PhotoID = '"+photoId+"'";
SqlDataReader thisReader = ReadQuantity.ExecuteReader();
// thisConnection.Close();
return thisReader;
}
}
}
------------------------------------------------------------------------
Could everyone tell me what happen to the sql connection if I don't
include the close function in my code. Would it close automatically
after timeout and would it be security threat???
Thanks for your time
Wing
I am writing the C# code function that take 2 parameters and access
the data in the MS SQL database and return a SqlDataReader object. In
my funtion, a SQL connection is established and open the connnection
before reading the data. Everything seem to be fine, but once I add the
close() function to close the sql connection, error come out. So I
leave out the close() function in my function. My code is showing below
-------------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string found = "c553a15f-15bd-4081-bd5d-89cfa2cc0505";
int photoId = 3;
SqlDataReader answer = ReadQuantity(found, photoId);
while(answer.Read())
{
int photoQuantity = Convert.ToInt16(answer["Quantity"]);
Console.WriteLine("\t{0}", photoQuantity);
}
}
static SqlDataReader ReadQuantity(string cartId, int photoId)
{
SqlConnection thisConnection = new SqlConnection
(@"Data Source=(local);Integrated Security=SSPI;" +
"Initial Catalog=PhotoDB");
thisConnection.Open();
SqlCommand ReadQuantity = thisConnection.CreateCommand();
ReadQuantity.CommandText = "SELECT Quantity FROM ShoppingCart WHERE
CartID = '"+cartId+"' AND PhotoID = '"+photoId+"'";
SqlDataReader thisReader = ReadQuantity.ExecuteReader();
// thisConnection.Close();
return thisReader;
}
}
}
------------------------------------------------------------------------
Could everyone tell me what happen to the sql connection if I don't
include the close function in my code. Would it close automatically
after timeout and would it be security threat???
Thanks for your time
Wing