1、连接数据库并返回DataTable
View Code
public DataTable GetDataByTable()
{
DataSet dataset = new DataSet();
DataTable datatable = null;
SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=Calamity_Data;Integrated Security=True");
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select top 50 * from calamity", sqlConnection);
sqlDataAdapter.Fill(dataset, "calamity");
if (dataset != null && dataset.Tables.Count > 0)
{
datatable = dataset.Tables[0];
}
}
catch (Exception e)
{
}
finally
{
sqlConnection.Close();
}
Console.WriteLine("Calling WCF Service,Transfer data using DataTable");
return datatable;
}
2、连接数据库文件的相对路径
SqlConnection sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database\\DatabaseWCF.mdf;Integrated Security=True;User Instance=True");
3、数组
交叉数组
for实现
int[][] arri = new
int[3][];
arri[0] = new
int[] { 1, 2 };
arri[1] = new
int[] { 3 };
arri[2] = new
int[] { 4, 5, 6 };
int rlen=arri.Length;
for (int i=0; i < rlen; i++)
{
int clen = arri[i].Length;
for (int j = 0; j < clen; j++)
{
Console.WriteLine(arri[i][j]);
}
}
Console.Read();
foreach实现
foreach (int[] i in arri)
{
foreach (int j in i)
{
Console.WriteLine(j);
}
}
Console.Read();
二维数组
View Code
int[,] arri = new
int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
int rlen = arri.GetLength(0);//行数
int clen = arri.GetLength(1);//列数
for (int i = 0; i < rlen; i++)
{
for (int j = 0; j < clen; j++)
{
Console.WriteLine("第{0}行{1}列,值为{2}", i, j, arri[i, j]);
}
}
Console.Read();
运行结果:
List应用