tehon3299 Posted February 22, 2003 Posted February 22, 2003 I am using the code below (VB) to insert things into an SQL database. I want to use this code on a C# page. How can I do this? Dim MyCommand As New SqlCommand Dim MyConnection As New SqlConnection Dim MySQLDataAdapter as New SqlDataAdapter Dim SessionCmd As String SessionCmd = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" & user & "', '" & savename.Value & "', 0)" MyConnection = New SqlConnection("server=(local);database=PictureShare;Trusted_Connection=yes") MyCommand = New SqlCommand(SessionCmd, MyConnection) MyCommand.Connection.Open() Try MyCommand.ExecuteNonQuery() Catch Exp As SQLException If Exp.Number = 2627 Else End If End Try MyCommand.Connection.Close() Quote Thanks, Tehon
Moderators Robby Posted February 22, 2003 Moderators Posted February 22, 2003 try this... MyConnection = new SqlConnection("server=127.0.0.1;database=PictureShare;Trusted_Connection=yes"); MySQLDataAdapter = new SqlDataAdapter(); SessionCmd String = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" & user & "', '" & savename.Value & "', 0)"; MyCommand = new SqlCommand(SessionCmd, MyConnection); MyCommand.Connection.Open(); try { MyCommand.ExecuteNonQuery(); } catch(SQLException Exp) { if (Exp.Number == 2627) { } else { } } MyCommand.Connection.Close(); Quote Visit...Bassic Software
*Experts* Bucky Posted February 22, 2003 *Experts* Posted February 22, 2003 If you want to use VB code in a C# project without having to convert it all, you could compile the VB code into a class library project that you can reference from your C# project. Then you can use all the methods and properties in it just like any class. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Experts* Volte Posted February 22, 2003 *Experts* Posted February 22, 2003 You don't even need to compile it; just create a new VB project in your solution and insert the class. Quote
tehon3299 Posted February 23, 2003 Author Posted February 23, 2003 I used that code and it tells me that MyConnection is not declared. I'm importing the same things for SQL in VB and C#. Do I need to import something else? Quote Thanks, Tehon
*Experts* Bucky Posted February 23, 2003 *Experts* Posted February 23, 2003 The declaration for these variables should be as follows: SqlConnection MyConnection = new SqlConnection("server=127.0.0.1;database=PictureShare;Trusted_Connection=yes"); SqlDataAdapter MySQLDataAdapter = new SqlDataAdapter(); string SessionCmd = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" + user + "', '" + savename.Value + "', 0)"; SqlCommand MyCommand = new SqlCommand(SessionCmd, MyConnection); Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Moderators Robby Posted February 23, 2003 Moderators Posted February 23, 2003 Oops, I made a mess of that one. :( Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.