20131209-数据库导入导出数据-sqlhelper-第十七天
[1] 导出数据
namespace _05导出数据
{
class Program
{
static void Main(string[] args)
{
string str = "Data Source=xy-pc;Initial Catalog=MyItcast;Integrated Security=True";
using (SqlConnection con=new SqlConnection(str))
{
string sql = "select TClassId,TClassName,tclassdesc FROM TblClass";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
using (SqlDataReader sda=cmd.ExecuteReader())
{
if (sda.HasRows)
{
//把数据库中的文件写入到文本文件中去
using (StreamWriter sw=new StreamWriter("1.txt"))
{
sw.WriteLine("{0},{1},{2}",sda.GetName(0),sda.GetName(1),sda.GetName(2));
while (sda.Read())
{
sw.WriteLine("{0},{1},{2}",sda["tclassid"],sda["tclassname"],sda["tclassdesc"]);
}
}
}
}
}
}
Console.WriteLine("搞定了");
Console.ReadKey();
}
}
}
[2]
namespace _06导入数据
{
class Program
{
static void Main(string[] args)
{
using (StreamReader sr=new StreamReader("1.txt"))
{
string line = sr.ReadLine();//读取第一行内容
string str = "Data Source=xy-pc;Initial Catalog=MyItcast;Integrated Security=True";
using (SqlConnection con=new SqlConnection(str))
{
string sql = "insert INTO TblClass(TClassName,TClassDesc)VALUES(@TClassName,@TClassDesc)";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();//打开数据库
//设置每个参数的类型
SqlParameter[] param =
{
new SqlParameter("@TClassName", System.Data.SqlDbType.NVarChar),
new SqlParameter("@TClassDesc", System.Data.SqlDbType.NVarChar)
};
cmd.Parameters.AddRange(param);
while ((line=sr.ReadLine())!=null)
{
string[]txts= line.Split(',');
string className = txts[1];
string classDesc = txts[2];
//给数组中的每个参数进行赋值
param[0].Value = className;
param[1].Value = classDesc;
cmd.ExecuteNonQuery();
}//end while
}//end using
}//end using
}//end 流
Console.WriteLine( "执行完了");
Console.ReadKey();
}
}
}
[3]App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add connectionString="Data Source=xy-pc;Initial Catalog=MyItcast;Integrated Security=True" name="strCon" />
</connectionStrings>
</configuration>
[4]
public class SQLHelper
{
private static readonly string str = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;
/// <summary>
/// 增删改方法
/// </summary>
/// <param name="sql">传sql语句</param>
/// <param name="param">如果sql语句带参数,则传参数,如果没有参数则不用传</param>
/// <returns></returns>
public static int ExeCuteNonQuery(string sql,params SqlParameter[] param)
{
using (SqlConnection con=new SqlConnection(str))
{
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
if (param!=null)
{
cmd.Parameters.AddRange(param);//把用户传过来的参数给cmdparameters了
}
return cmd.ExecuteNonQuery();//完事
}
}
}
public static object ExecuteScalar(string sql,params SqlParameter[]param)
{
using (SqlConnection con=new SqlConnection(str))
{
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
if (param!=null)
{
cmd.Parameters.AddRange(param);
}
return cmd.ExecuteScalar();
}
}
}
public static SqlDataReader ExecuteReader(string sql,params SqlParameter[]param)
{
SqlConnection con = new SqlConnection(str);
using (SqlCommand cmd=new SqlCommand(sql,con))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
con.Close();
con.Dispose();
throw ex;//有可能会出现异常,但是不确定异常是什么
}
}
}
}
}
[5]sql参数
string sql = "insert into tblstudent(tsname,tsgender,tsage,tclassid)values(@name,@gender,@age,@classid)";
SqlParameter[] param =
{
new SqlParameter("@name",name),
new SqlParameter("@gender",gender),
new SqlParameter("@age",age),
new SqlParameter("@classid",classid)
};