I have written testcases for Database connection and select and update records.
Select Testcase -
using (SqlConnection cnn = new SqlConnection(connection))
{
cnn.Open();
cmd = new SqlCommand("AccessionDetails", cnn);
cmd.Parameters.Add(new SqlParameter("#CURRENTTIME", now));
cmd.Parameters.Add(new SqlParameter("#TIMEINTERVAL", Int32.Parse(ConfigurationManager.AppSettings["interval"])));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
Assert.IsTrue(dt.Rows.Count > 0);
}
Update Testcase -
using (SqlConnection cnn = new SqlConnection(connection))
{
cnn.Open();
cmd = new SqlCommand("AccessionDetails", cnn);
cmd.Parameters.Add(new SqlParameter("#CURRENTTIME", now));
cmd.Parameters.Add(new SqlParameter("#TIMEINTERVAL", Int32.Parse(ConfigurationManager.AppSettings["interval"])));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
cmd = new SqlCommand("AccessionUpdate", cnn);
cmd.Parameters.Add(new SqlParameter("#ACCESSIONID", dt.Rows[1]["AccessionID"].ToString()));
cmd.CommandType = CommandType.StoredProcedure;
int affectedRows = cmd.ExecuteNonQuery();
Assert.IsTrue(dt.Rows.Count > 0);
}
I am using the same code where i implemented in the actual project code.
My select and update queries returns large set of values dynamically . So i cant not take single value and test it out in each Iteration.
My task is to Mock the above queries. Please help me in implementing the mocks.
Related
I'm using the flowing code to call the oracle function with the return value,
but it returns null always
OracleCommand cmd = new OracleCommand();
using (OracleConnection cnn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=321352427544)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)));User ID=abc;Password=123;"))
{
cmd.Connection = cnn;
cmd.CommandText = "GetEmp";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("P_EMP_ID", OracleDbType.Int32).Value = 4241;
cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;
cnn.Open();
cmd.ExecuteNonQuery();
string Count = (string)cmd.Parameters["return_value"].Value;
cnn.Close();
}
the problem was solved in two ways by
add cmd.BindByName = true;
add the return parameter in the first: cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;
public Section SectionView(object id, SqlConnection conn)
{
using (SqlCommand cmd = new SqlCommand())
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("TMR_SECTION_VIEW", conn);
SqlDataAdapter da = new SqlDataAdapter("data", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "TMR_SECTION_VIEW";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#sectionID", id.ToString);
cmd.Parameters.AddWithValue("#name", name);
da.SelectCommand = cmd;
DataTable dt = new DataTable();
DataTable dtbl = new DataTable();
sqlda.Fill(dtbl);
cmd.ExecuteNonQuery();
return id;
}
}
My stored procedure is:
ALTER PROCEDURE [dbo].[TMR_SECTION_VIEW]
#sectionID int, #name varchar(100)
AS
BEGIN
SELECT *
FROM Section
WHERE sectionid = #sectionID
END
You have things a little bit out of order...
You should be specifying the command name on the SqlCommand (not the DataAdapter) and you should be telling the DataAdapter to use the SqlCommand.
I'd change this to allow you to specify the Stored Procedure name as a parameter when you call the function:
public Section SectionView(object id, SqlConnection conn, string sql = String.Empty)
{
if (!String.IsNullOrEmpty(sql))
{
if (conn.State == ConnectionState.Closed) conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("#sectionID", id.ToString);
cmd.Parameters.AddWithValue("#name", name);
SqlDataAdapter da = new SqlDataAdapter
{ SelectCommand = cmd };
DataTable dtbl = new DataTable();
sqlda.Fill(dtbl);
return id;
}
}
}
You duplicated a lot of things (like setting the CommandType of your SqlCommand) and you created DataTable dt without using it, so I removed it from the sample in my answer.
So what's happening here is that you're specifying a sql string as a parameter (which can be a normal SQL query or a stored procedure) and you're building a SqlCommand with it that has parameters.
Using that SqlCommand, you're creating a DataAdapter having the SqlCommand as its SelectCommand and then you're using that DataAdapter to fill the DataTable.
NOTE: You don't need to execute SqlCommand.ExecuteNonQuery() when retrieving data as the DataAdapter.Fill() function basically does that for you.
ExecuteNonQuery would be useful when inserting or updating data - not when reading data.
This question already has answers here:
Error: Procedure or function expects parameter which was not supplied
(2 answers)
Closed 5 years ago.
String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("#CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("#Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString();
cmd.Parameters.Add("#disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString();
cmd.Parameters.Add("#SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString();
cmd = new SqlCommand("UserManagement", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.Visible = true;
Your code is a big mess. To me it looks like you've just used copy and paste from a bunch of other places and don't quite really understand what's going on in there.
It should be something like this:
String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
DataTable dt = new DataTable();
using(var con = new SqlConnection(strConnString))
{
using(var cmd = new SqlCommand("UserManagement", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("#Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString();
cmd.Parameters.Add("#disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString();
cmd.Parameters.Add("#SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString();
using(var da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
da.Fill(dt);
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.Visible = true;
This line assigned new instance of SqlCommand to cmd for second time instead of using existing SqlCommand which contains declared parameters, therefore removing all parameters already declared above:
cmd = new SqlCommand("UserManagement", con);
The correct way to manage SqlCommand with parameterized stored procedure should be like this, with using statement to manage system resources during execution (better to use try...catch...finally block in case you want to handle SqlException):
String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
var dt = new DataTable();
using (var con = new SqlConnection(strConnString))
{
con.Open();
using (var cmd = new SqlCommand("UserManagement", con))
{
cmd.Parameters.Add("#CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString();
cmd.Parameters.Add("#Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString();
cmd.Parameters.Add("#disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString();
cmd.Parameters.Add("#SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString();
cmd.CommandType = CommandType.StoredProcedure;
// using SqlDataAdapter
using (var da = new SqlDataAdapter)
{
da.SelectCommand = cmd;
da.Fill(dt);
}
// using DataTable.Load directly
// dt.Load(cmd.ExecuteReader());
}
con.Close();
}
// other stuff
NB: In short you can replace the second assignment of cmd in question using cmd.CommandText = "UserManagement";, hence it just supplying stored procedure name instead assigning another instance of SqlCommand. Also instead of ExecuteNonQuery() try using ExecuteReader() with DataTable.Load() method.
My application needs to get data from a store procedure. In a query program the stored procedure works fine;
exec MyStoredProdure #Prama1 = '01'
In WPF it times out. Here is my code;
using (SqlConnection con = new SqlConnection(ConString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Prama1", "01");
cmd.CommandText = "MyStoredProdure";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("MyStoredProdure");
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
//Do code
}
con.Close();
}
It is timing out right on sda.Fill(dt). It should be noted that Select and inserts work perfectly fine.
I believe you're missing the sda.SelectCommand = cmd; before the fill.
How to use a DataAdapter with stored procedure and parameter
I have this error at the call af stored procedure... Why?
public void InsertVideo()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());
MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
cmd.Parameters.Add(new MySqlParameter("in_title", Title));
cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));
cmd.ExecuteNonQuery();
}
because you forget to OPEN the connection, before calling ExecuteNonQuery, call conn.Open() first,
conn.Open();
cmd.ExecuteNonQuery();
Your complete solution
public void InsertVideo()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());
MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open()
cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
cmd.Parameters.Add(new MySqlParameter("in_title", Title));
cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));
cmd.ExecuteNonQuery();
}