Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m programming in c# window application my question is ..i just want to do ..my patch should run only when my computer name match which i have define in programming ..ie if my computer name is SINGH then only my patch should run other it should not run

so pls tell me how to define computer name in my programming and provide code

What I have tried:

SqlDataAdapter SQL = new SqlDataAdapter("SELECT * from EMP WHERE VCH_DATE BETWEEN convert(datetime,'" + dateTimePicker1.Text + "',103) AND convert(datetime,'" + dateTimePicker2.Text + "',103) GROUP BY A.AC_NAME,B.STATE,B.ZONE_NAME ", CON);
            DataTable DT;
            DT = new DataTable();
            SQL.Fill(DT);
            dataGridView1.DataSource = DT;
Posted
Updated 26-Aug-19 1:56am

Try:
C#
Console.WriteLine(Environment.MachineName);


But ... don't do SQL like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
In this case, it's not that bad, but it still leaves you open to mistakes, if the SQL server is not configured for the same locale and default date format as your computer. "01-02-03" can be misinterpreted as "first Feb 2003", "Second Jan 2003", "Third Feb 2001" because you pass text. Pass the DateTime directly, and there is no chance of error.

And BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
You can use Environment.MachineName, see: Environment.MachineName Property (System) | Microsoft Docs[^]
 
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