user1789585
user1789585

Reputation: 21

Read a row from SQL through .net using MATLAB

I am trying to read a specific row from a SQL table through .net using MATLAB. I am using the following code to that

import System.Data.SqlClient.*   
import DataTable.*  
NET.addAssembly('System.Data');  
sqlconn = System.Data.SqlClient.SqlConnection();  
sqlcmd = sqlconn.CreateCommand();      
conn = SqlConnection('Data Source=B39Q5M1;Initial Catalog=AIR;integrated  
i=1;  
list = [];  
conn.Open();  
q = SqlCommand(sqlstring, conn);   
r = q.ExecuteReader();  
while (r.read())  
r.GetString(0)  
end  
end

But this fetches a single element from the row. At least in this case i have a workaround of getting each element and making it an array. But the bigger problem is I dont know whether the table value is int/string. In this case I cant blindly use Getstring coz it throws me an error if the value is int and not string. Please let me know if there is any methods to fetch complete row. Or is there any workaround for this? I searched a lot in the sites but all the examples use GetString(0). Thanks in Advance!

Upvotes: 2

Views: 635

Answers (1)

Merr Leader
Merr Leader

Reputation: 737

Could you format it a little more like this: http://support.winhost.com/KB/a647/how-to-query-an-sql-database-with-net-sql-data-provider.aspx

There should be an easier method to build the SQL statement you want to use.

http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx This is the library for datatable for C#, all of the get and table commands are here, you can definitely retrieve data from a database whether or not it is a String or Int.

It looks like GETROWTYPE() is a method that will get you the type, maybe you could use this to determine whether or not you need to use a GetString or not, until you figure out how you want to tackle the main issue.

Upvotes: 0

Related Questions