Suppose that Sql database is StudentInfo and Table name is Registration
ID----------Name---------------Email---------------------------PhoneNo
1 Munasunghe amilamunasinghe@yahoo.com 0717069425
2 Liyanarachchi hareshliya6@gmail.com 0756706352
protected void Page_Load(object sender, EventArgs e)
{
string query = "select ID, Name, Email, PhoneNo from Registration";
SqlCommand cmd1 = new SqlCommand(query);
DataTable dt1 = GetData(cmd1);
int rowcount = dt1.Rows.Count;
/* I want to assing dt1 datatable data to 2 dimensional array*/
}
The function GetData is used to get data from the Database.
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
Please give an example code to help me.
object[,] Target = new object[dt1.Rows.Count, dt1.Columns.Count];
int RowCount = 0;
foreach(DataRow dr in dt1.Rows)
foreach(DataColumn dc in dt1.Columns)
{
Target[RowCount, dc.Ordinal] = dr[dc];
RowCount++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.