Click here to Skip to main content
16,011,383 members
Home / Discussions / C#
   

C#

 
GeneralRe: Your chance to provide input for a book! Pin
Nick Parker3-Dec-03 5:33
protectorNick Parker3-Dec-03 5:33 
GeneralRe: Your chance to provide input for a book! Pin
Not Active3-Dec-03 3:09
mentorNot Active3-Dec-03 3:09 
GeneralRe: Your chance to provide input for a book! Pin
Tom Archer3-Dec-03 5:23
Tom Archer3-Dec-03 5:23 
GeneralRe: Your chance to provide input for a book! Pin
Not Active3-Dec-03 9:29
mentorNot Active3-Dec-03 9:29 
GeneralRe: Your chance to provide input for a book! Pin
Tom Archer3-Dec-03 9:36
Tom Archer3-Dec-03 9:36 
GeneralRe: Your chance to provide input for a book! Pin
MtnBiknGuy5-Dec-03 12:24
MtnBiknGuy5-Dec-03 12:24 
GeneralAdding a row to SQL table using C# Pin
Larry Antram2-Dec-03 14:25
Larry Antram2-Dec-03 14:25 
GeneralRe: Adding a row to SQL table using C# Pin
Heath Stewart2-Dec-03 16:43
protectorHeath Stewart2-Dec-03 16:43 
You're not using the SQL statements correctly in .NET if you think INSERTs (or any other command) is hard. Take a look at the documentation for SqlParameter and SqlCommand.Parameters. You don't have to worry about properly escaping param values and can work with input, output, and return params with no problem:
SqlConnection conn = new SqlConnection(connectionString);
try
{
  SqlCommand cmd = conn.CreateCommand();
  cmd.CommandText = "INSERT INTO MyTable (ID, Name, Birthday) " +
    "VALUES(@ID, @Name, @Birthday)";
  cmd.Parameters.Add("@ID", SqlDbType.Int);
  cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 40);
  cmd.Parameters.Add("@Birthday", SqlDbType.DateTime);
  string[] names = new string[] {"Dad", "Mom", "Brother"};
  conn.Open();
  for (int i=0; i<names.Length; i++)
  {
    cmd.Parameters["@ID"].Value = i;
    cmd.Parameters["@Name"].Value = names[i];
    cmd.Parameters["@Birthday"].Value = DateTime.Now.AddYears(i * -10);
    cmd.ExecuteNonQuery();
  }
}
finally
{
  if (conn != null) conn.Close();
}
This is just an example to show you the power of parameterized statements. The same is done with SqlCommands when using the SqlDataAdapter.

The other way is to create a DataSet, build the schema (or create a strongly-typed DataSet using VS.NET's DataSet designer (or other data designers, like dragging and dropping a table from the connections tab), or the xsd.exe utility), and add rows to that, which you then call SqlDataAdapter.Update, but you'll still need parameterized queries.

This is how ADO.NET works and - if you architect your solution right - can be much better than the old ADO way (for example, DataSets are very good at tracking changes, dealing with relationships, and even identity columns).

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: Adding a row to SQL table using C# Pin
Larry Antram2-Dec-03 16:59
Larry Antram2-Dec-03 16:59 
GeneralAny idea on this problem Pin
Alex Korchemniy2-Dec-03 13:18
Alex Korchemniy2-Dec-03 13:18 
GeneralRe: Any idea on this problem Pin
Heath Stewart2-Dec-03 16:57
protectorHeath Stewart2-Dec-03 16:57 
GeneralControl clipping Pin
Gary Kirkham2-Dec-03 10:38
Gary Kirkham2-Dec-03 10:38 
GeneralRe: Control clipping Pin
Alex Korchemniy2-Dec-03 11:04
Alex Korchemniy2-Dec-03 11:04 
GeneralAdding class values Pin
MrEyes2-Dec-03 7:32
MrEyes2-Dec-03 7:32 
GeneralRe: Adding class values Pin
ankita patel2-Dec-03 8:20
ankita patel2-Dec-03 8:20 
GeneralProblems calling a COM+ component Pin
Ivan Fernandez2-Dec-03 7:07
Ivan Fernandez2-Dec-03 7:07 
GeneralRe: Problems calling a COM+ component Pin
Bruce Duncan2-Dec-03 8:08
Bruce Duncan2-Dec-03 8:08 
GeneralRe: Problems calling a COM+ component Pin
Ivan Fernandez2-Dec-03 8:16
Ivan Fernandez2-Dec-03 8:16 
GeneralRe: Problems calling a COM+ component Pin
Heath Stewart2-Dec-03 8:56
protectorHeath Stewart2-Dec-03 8:56 
Generalvideo conference Pin
notaclue2-Dec-03 4:58
notaclue2-Dec-03 4:58 
GeneralMoving a file from one server to another Pin
chubbysilk2-Dec-03 4:12
chubbysilk2-Dec-03 4:12 
GeneralRe: Moving a file from one server to another Pin
Heath Stewart2-Dec-03 4:32
protectorHeath Stewart2-Dec-03 4:32 
GeneralGDI - Creating Images Pin
MrEyes2-Dec-03 3:31
MrEyes2-Dec-03 3:31 
GeneralRe: GDI - Creating Images Pin
Heath Stewart2-Dec-03 4:05
protectorHeath Stewart2-Dec-03 4:05 
GeneralRe: GDI - Creating Images Pin
MrEyes2-Dec-03 7:25
MrEyes2-Dec-03 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.