working with SqlCommand and insert

admin

Administrator
Staff member
i want ot use the SqlParameter object
i am trying t o do this :
sqlCmd.Parameters.Add(New SqlParameter("@" + Ctrl.Controls.Item(i).ID, SqlDbType.VarChar, 50))
so if new SqlParameter added can i use it to do an insert to the db?
thnaks in advance
pelegsure its so easy

string sql = "INSERT INTO TableName (Column1, Column2) VALUES (@Clmn1, @Clmn2)";
SqlCommand sqlCmd = new SqlCommand(sql, Conn);
sqlCmd.sqlCmd.Parameters.Add("@Clmn1", SqlDbType.VarChar, 50);
sqlCmd.sqlCmd.Parameters.Add("@Clmn2", SqlDbType.VarChar, 50);

you can directly assign values to them like this
sqlCmd.sqlCmd.Parameters.Add("@Clmn1", SqlDbType.VarChar, 50).Value = TextBox1.Text;fisrt thanks alot
but why do u do :
sqlCmd.sqlCmd???
1 more question assuming i am inserting all the fields by order then i can do :
insert into TableName vALUES(the values here will be execlly like in the db order of tables)

thnaks i nadvance
pelegfisrt sorry for sqlCmd.sqlCmd didnt mean it :)
second, i dont think you must use the same order in db, but i think you must use the same order you used when assigning values to them.
INSERT INTO Table (Column1, Column3, Column2) Values (vColum1, vColumn2, vColumn2)
but i'm not sure actualy but i think thats right.but what do i nedd to write instead of sqlCmd.sqlCmd???sqlCmd.Parameters.Add("@Clmn1", SqlDbType.VarChar, 50);

i just wrote sqlCmd twise :rolleyes:its cool to bbuld a dynamic insert like this
 
Back
Top