Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Want Query to Lock one process..
Wen we go to next month process first we want to lock previous month calculation by creating settings..
This should be for whole table we have set common lock procedure.
Need query for Sql2005
Posted
Comments
[no name] 5-Mar-12 10:38am    
Your question is not clear
BobJanova 5-Mar-12 11:24am    
You can lock tables within a single transaction trivially. I think you mean permanently locking a whole table as a read only archive, in which case you should remove the UPDATE and DELETE (and ALTER etc) privileges from the application user on that table.
Charuroopa 5-Mar-12 11:33am    
not asking for permanent locking..
if we want to calculate next month calculation first we have to lock previous once right? for that i need query..
and also we want to lock the financial year while we move to next year process..
Whether we want to maintain one separate table for this process or how we have to proceed it further..
Charuroopa 6-Mar-12 1:06am    
What i m doing is payroll project...
For that i am asking..
i hope now u wil come to know for what i am asking for...
Charuroopa 7-Mar-12 1:08am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Security;

namespace lockprocedure
{
public partial class _Default : System.Web.UI.Page
{

SqlDataReader reader;
SqlConnection conn;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string connectionString="Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=******;Password=*********";
conn = new SqlConnection(connectionString);
string updateSql = "update lock_table set key_value=1 where sal_date='2012-02-25 00:00:00.000'";
cmd = new SqlCommand(updateSql, conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();

}
catch (Exception)
{

}
finally
{
conn.Close();
}
MessageBox("Locked the previous month calculation");
}
private void MessageBox(string message)
{
if (!string.IsNullOrEmpty(message))
{
Response.Write("<script type=\'text/javascript' language=\'javascript\'>");
Response.Write("alert('" + message + "');");
Response.Write("</script>");
}
}


protected void Button2_Click(object sender, EventArgs e)
{
int key_value;
string connectionstring="Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=******;Password=******";
conn = new SqlConnection(connectionstring);
conn.Open();
// string updateSql = "update salary set key_value=1 where process_datetime='2012-02-25 00:00:00.000'";
SqlCommand cmd = new SqlCommand(("select key_value from lock_table where sal_date='2012-02-25 00:00:00.000' "), conn);
// cmd = new SqlCommand(updateSql, conn);
cmd.Connection = conn;


reader = cmd.ExecuteReader();

//reader.Read();

/*if (reader.HasRows)
{
}*/
while (reader.Read())
{
// key_value = reader.GetOrdinal("key_value");
reader.Read();
reader["key_value"].ToString();
//key_value = reader.GetOrdinal("key_value");
//Console.WriteLine(reader.GetInt32(1));
/* if (key_value == 0)
{
MessageBox("Lock the previous Month Calculation");
}
else
{
MessageBox("Can proceed further");
}
reader.Close();*/

}

key_value = reader.GetOrdinal("key_value");
// int key_value =reader.GetInt32(reader.GetOrdinal("key_value"));

if (key_value == 0)
{
MessageBox("Lock the previous Month Calculation");
}
else
{
MessageBox("Can proceed further");
}
reader.Close();

}

}
}

-------------------------------------------------------------------------------
This is my code for lock procedure.. In this i am getting an error invalid attempt to read when no data is present. c# and also reader is not working properly because after i locked the previous month calculation again it is asking to lock

1 solution

This is ENTIRELY dependant on your business requirements, procedures, and your database design. What you do is entirely up to you. You don't NEED to "lock" anything unless your design requires it, depending on what your definition of "lock" is.

It may be as simple as taking the records from the production tables and moving them to archive tables of some kind. I, and nobody here, has any idea of what your requirements and design are, so it's impossible to say exactly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900