Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Dear All

How can insert globle variable value in 5 table(sql server 2008 r2) at a time
Posted

  1. .NET does not have the concept of "global variables", which is actually very good, because having global variables means bad programming style, especially bad for code maintenance;
  2. tables don't store "variables", they store data.

Therefore, it's not just you cannot do it; it looks like your question doesn't make sense. Perhaps you meant something else, but then you have to explain it properly, in some cultured, accurate and comprehensible way.

—SA
 
Share this answer
 
v2
Comments
Arjunwalmiki 14-Feb-14 1:56am    
for e.g i am save one id in variable and save variable data want to save in multiple table so how can it is possible if you have some code pls give to me
Maciej Los 14-Feb-14 2:35am    
Good point! +5
Sergey Alexandrovich Kryukov 14-Feb-14 10:37am    
Thank you, Maciej.
—SA
If i understand you well, you need to use ExecuteNonQuery[^] method for SqlCommand[^] object.

Your query should looks like:
a) text variable:
C#
string myvalue = "A";
string mycommand = "INSERT INTO Table1(FieldName) VALUES('" + myvalue + "')";
b) numeric varaible:
C#
int myvalue = 0;
string mycommand = "INSERT INTO Table2(FieldName) VALUES(" + myvalue.ToString() + ")";


How to create SqlCommand? Please see this example: http://csharpdotnetfreak.blogspot.com/2012/05/sqlcommand-executenonquery-example-c-vb.html[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Feb-14 10:38am    
Instructive, probably too much for who-knows-what kink of question, a 5.
—SA
Maciej Los 14-Feb-14 14:43pm    
Thank you, Sergey ;)
thanyou

C#
int myvalue = 0;
string mycommand = "INSERT INTO Table2(FieldName) VALUES(" + myvalue.ToString() + ")";


this code is work very fineeeeeeeeeeee thank you
 
Share this answer
 
Comments
Maciej Los 14-Feb-14 2:55am    
This is not an answer. Please, delete it to avoid down-voting. To post comment, use "Have a Question or Comment" widget.
Arjunwalmiki 14-Feb-14 3:25am    
okey
int myvalue = 0;
string mycommand = "INSERT INTO Table2(FieldName) VALUES(" + myvalue.ToString() + ")";
this code is working

but can you know if i am use store procedure than how can insert + myvalue.ToString() + in to 5 table
C#
int ScriptId = 0;
               string sql = "Select Max(ScriptId) from tbl_Script";
               SqlCommand cmd = new SqlCommand(sql, connection);
               using (var reader = cmd.ExecuteReader())
               {
                   while (reader.Read())
                   {

                       if (reader["ScriptId"] == System.DBNull.Value)
                       {
                           ScriptId += 1;
                       }
                       else
                       {
                           ScriptId = ScriptId++;
                       }
                   }
               }
                   globalScriptid = ScriptId.ToString();
                   SqlCommand cmd4 = new SqlCommand("tblScriptInsert", connection);
                   cmd4.Parameters.Add("@Ip_Address", SqlDbType.VarChar).Value = ScriptId.ToString();
                   cmd4.CommandType = CommandType.StoredProcedure;


this is my code i am i am get error in ( if (reader["ScriptId"] == System.DBNull.Value) IndexOutOfRangeException was unhandled pls help
 
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