Skip to content

Commit

Permalink
update test code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsksxs360 committed Nov 5, 2016
1 parent d4a6218 commit 773c0a7
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,56 @@ class Program
static void Main(string[] args)
{
//建立SqlHelper对象(包含用户名、密码)
//SqlHelper sqlHelper = new SqlHelper(".", "TestDB", "sa", "12345678");
//SqlHelper sqlHelper = new SqlHelper("127.0.0.1", "TestDB", "sa", "12345678");
//建立SqlHelper对象(不包含用户名、密码)
//SqlHelper sqlHelper = new SqlHelper(@"(localdb)\ProjectsV12", "test");
SqlHelper sqlHelper = new SqlHelper("127.0.0.1", "TestDB");

//通过表名获取数据表
//DataTable stuTable = sqlHelper.GetTable("student", 50);
//PrintTable(stuTable);
DataTable stuTable = sqlHelper.GetTable("student", 50);
PrintTable(stuTable);
//通过sql语句获取数据表
//DataTable stuTable = sqlHelper.GetTable("select * from student where sex=N'男'");
//PrintTable(stuTable);
DataTable stuTable2 = sqlHelper.GetTable("select * from student where sex=N'男'");
PrintTable(stuTable2);

//按流的方式单向读取数据(使用SqlDataReader)
//SqlDataReader sqlDataReader = sqlHelper.GetDataStream("select * from student where sex=N'男'");
//while (sqlDataReader.Read())
//{
// Console.WriteLine(sqlDataReader["sid"] + "\t" +
// sqlDataReader["name"] + "\t" + sqlDataReader["age"] +
// "\t" + sqlDataReader["sex"]);
//}
SqlDataReader sqlDataReader = sqlHelper.GetDataStream("select * from student where sex=N'男'");
while (sqlDataReader.Read())
{
//获取指定字段的值
string id = sqlDataReader["sid"].ToString();
string name = sqlDataReader["name"].ToString();
string sex = sqlDataReader["sex"].ToString();
string score = sqlDataReader["score"].ToString();
Console.WriteLine(id + "\t" + name + "\t" + sex + "\t" + score);
}
sqlHelper.CloseConnection();

//执行一条SQL语句
//sqlHelper.ExecuteSqlCommand("insert into student(sid,name,age,sex) values(102,'hong',20,N'女')");
//DataTable stuTable = sqlHelper.GetTable("student", 50);
//PrintTable(stuTable);
sqlHelper.ExecuteSqlCommand("insert into student(sid,name,sex,score) values(102,'hong',N'女',78.5)");
DataTable stuTable3 = sqlHelper.GetTable("student", 50);
PrintTable(stuTable3);

//添加数据到指定DataSet中(添加到一张表)
//DataSet dataSet = new DataSet();
//sqlHelper.AddDataToDataSet(dataSet, "select * from student", "student");
//PrintTable(dataSet.Tables["student"]);
DataSet dataSet = new DataSet();
sqlHelper.AddDataToDataSet(dataSet, "select * from student", "student");
PrintTable(dataSet.Tables["student"]);
//添加数据到指定DataSet中(添加到多张表)
//DataSet dataSet = new DataSet();
//sqlHelper.AddDataToDataSet(dataSet, new List<string> { "select * from student", "select * from teacher" }, new List<string> { "student", "teacher" });
//PrintTable(dataSet.Tables["student"]);
//PrintTable(dataSet.Tables["teacher"]);

//修改student表的年龄,批量提交对数据表进行的修改
//DataTable stuTable = sqlHelper.GetTable("select * from student");
//foreach (DataRow row in stuTable.Rows)
// row["age"] = int.Parse(row["age"].ToString()) - 1;
//sqlHelper.UpdateTable(stuTable, "select * from student");
//修改student表的分数,批量提交对数据表进行的修改
DataTable tempTable = sqlHelper.GetTable("select * from student");
foreach (DataRow row in tempTable.Rows)
row["score"] = double.Parse(row["score"].ToString()) - 1;
sqlHelper.UpdateTable(tempTable, "select * from student");

//修改student表的年龄,批量提交对数据表进行的修改
//修改student表的分数,批量提交对数据表进行的修改
//DataSet dataSet = new DataSet();
//sqlHelper.AddDataToDataSet(dataSet, "select * from student", "student");
//foreach (DataRow row in dataSet.Tables["student"].Rows)
// row["age"] = int.Parse(row["age"].ToString()) + 1;
// row["score"] = int.Parse(row["score"].ToString()) + 1;
//sqlHelper.UpdateTable(dataSet, "student", "select * from student");
}

Expand Down

0 comments on commit 773c0a7

Please sign in to comment.