Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi my name is vishal i am translating/interpreting a vb6 code adodb with Ms access into c# code with sql server 2008.
Given below is vb6 code:
VB
Dim sItemData As String
Dim strData As String
Dim strOutData As String
Dim strConnect As String
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Prices.mdb;Persist Security Info=False"
    Dim strPath As String
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Persist Security Info=False;Data Source=" & strPath & _
      "; Mode=Read|Write"
    Dim rs As New ADODB.Recordset
strData = "Item = '" & sItemData & "'"
   rs.Open "select * from prices", strConnect, adOpenKeyset, adLockOptimistic
    rs.Find strData
    strOutData = rs.Fields("Price")

Given below is my c# code translation/interpretation of above vb6 code:
C#
string sItemData="";
        string strOutData;
string strData;
            strData = "Item = '" + sItemData + "'";
 SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Winsock;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string root = ("Select * from Prices");
            SqlCommand cmd = new SqlCommand(root);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                
                strOutData = dr[1].ToString();
            }

every thing goes okay except i am unable to translate/interpret given vb6 code line:
VB
rs.Find strData

into c# code with sql server 2008.
Can anyone help me please! Am going in right direction in translating above vb6 code into c# code with sql server? Tell me what is equivalent of rs.Find in c# with sql server 2008? Tell me what modifications must i do in my c# code with sql server 2008. Any help/guidance in solving of this problem would be greatly appreciated! Can anyone help me !
Posted

The Find method is moving to a record that matches the criteria in strData - Reference[^]

Personally I would add a where clause to the sql query ...
"Select * from Prices where item = @itemdata"

and add a parameter to cmd

And yes you are going in the right direction moving away from VB6!! C# is a good choice if it fits your needs. You didn't have to move away from Access at the same time, but it's is also a Good Move
 
Share this answer
 
Well there's a lot of ways of doing it,including the best in my opinion that would be including the search expression in the select clause,using parameters. But using your code,i'll try something like this:
C#
while (dr.Read())
            {
                if (dr[1].ToString()==strData)
                {
                    strOutData = dr[1].ToString();
                {
            }
 
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