Click here to Skip to main content
12,453,873 members (59,705 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
I am add the 0(Zeros)to sql table by using sql statement

select REPLICATE ('0',8-DATALENGTH (EmployeeCode ))+EmployeeCode as 'EmpCode' from TBL_DKCSV

But how to pass sqlcommnd in C#.net

Please help me..
Posted 3-Dec-12 19:29pm
Baroor716
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

 string szQuery = "select REPLICATE ('0',8-DATALENGTH (EmployeeCode ))+EmployeeCode as 'EmpCode' from TBL_DKCSV"; //This might need some escaping

//Set connection to your database connection

SqlCommand cmd = new SqlCommand(szQuery, connection);
 
cmd.ExecuteNonQuery();
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If you are looking for a function in C# which is similar to REPLICATE in SQL server, you can use Padding strings[^]

If you are looking to show data from database in your application take a look at this article
Using ADO.NET for beginners[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi dear,
 
private void FxREPLICATE()
{
  SqlConnection objConn1 = new SqlConnection("Data Source= Sql Server Name ;Initial Catalog= Database Name ;user id=sa; password=123;"); // Write you sql server database name and necessary option.
  string sql = "select REPLICATE ('0',8-DATALENGTH (EmployeeCode ))+EmployeeCode as 'EmpCode' from TBL_DKCSV";
  SqlCommand cmd = new SqlCommand(sql, objConn1);
  objConn1.Open();
  cmd.ExecuteNonQuery();
  objConn1.Close();
 }
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100