mssqlserver导入到数据库中插入文字乱码难题一蹴即至措施

private void btninsert_Click(object sender, 伊芙ntArgs eState of Qatar{if
(odimport.ShowDialog(卡塔尔(قطر‎ == DialogResult.OKState of Qatar//准备接到文件;{using
(FileStream filestream = File.OpenRead(odimport.FileNameState of QatarState of Qatar{using
(StreamReader streamreader = new
StreamReader(filestream卡塔尔卡塔尔国{//创设连接字符串及树立通道;string connstr =
ConfigurationManager.ConnectionStrings[constr].ConnectionString;using
(SqlConnection conn = new SqlConnection(connstr)){conn.Open();using
(SqlCommand cmd = conn.CreateCommand()){cmd.CommandText = insert into
Phonenumber(Phone,LocalID,LocalAddress,Phonestyle)
values(@Phone,@LocalID,@LocalAddress,@Phonestyle);string line =
null;while ((line = streamreader.ReadLine()) !=
null卡塔尔(قطر‎{//转变每一行数据为数组并用split分割字符串,获取个字段消息;string[]
strs = line.Split(‘ ‘);string Phone = strs[0];int LocalID =
Convert.ToInt32(strs[1]);string LocalAddress = strs[2];string
Phonestyle = strs[3];

cmd.Parameters.Clear();cmd.Parameters.Add(new SqlParameter(Phone,
Phone));cmd.Parameters.Add(new SqlParameter(LocalID,
LocalID));cmd.Parameters.Add(new SqlParameter(LocalAddress,
LocalAddress));cmd.Parameters.Add(new SqlParameter(Phonestyle,
Phonestyle));cmd.ExecuteNonQuery();}}

}}}MessageBox.Show(导入成功State of Qatar;}

public static DataTable ExecuteDataTable(string cmdText,
params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
return ExecuteDataTable(conn, cmdText, parameters);
}
}

蛋疼啊、淡定!

public static object ExecuteScalar(SqlConnection conn, string cmdText,
params SqlParameter[] parameters)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}

通过一上午的折磨得出了一条结论:编制程序真是一件很令人蛋疼的事!忍住疼痛,你就变得淡定了!

public static object ExecuteScalar(string cmdText,
params SqlParameter[]澳门新萄京, parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
return ExecuteScalar(conn, cmdText, parameters);
}
}

下一场上网搜了一大堆关于数据库中插入文字乱码难点的相干材质;不看万幸,越看越晕,竟然还也会有位老兄说那是mssqlserver的数据爱慕,什么加密字段,靠理念微软也不会这么也不会如此bt,还有个别正是数据库字符集的主题材料,小编试了试得出结论:都以闲聊!

public static DataTable ExecuteDataTable(SqlConnection conn, string
cmdText,
params SqlParameter[] parameters)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
}

mssqlserver导入到数据库教程中插入文字乱码难题解决措施,先看一下自己的导入代码,应该是从未有过别的难题了。

public static object ToDBValue(this object value)
{
return value == null ? DBNull.Value : value;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图