我想通过C#将txt文件里的地理位置信息添加到数据库中,由于数据库里的数据类型是geometry,总是报错。麻烦大神帮我看一下代码哪里有问题?
1;sow;7acre;'POINT(116.360413 40.008438)'
2;sow;3acre;'POINT(116.360413 40.008438)'
3;sow;4acre;'POINT(116.360413 40.008438)'
4;sow;5acre;'POINT(116.360413 40.008438)'
上面是txt文本信息
public void Button1_Click(object sender, EventArgs e)
{
string path = @"D:\db_example\a.txt";
StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read), System.Text.Encoding.UTF8);
SqlConnection con = new SqlConnection("Server=localhost;user id=sa;pwd=liujiang;database=Plan");
string sql = "insert into Plan_info values (@ProjectID,@ProjectType, @TargetDo,geometry::STGeomFromText(@d,4326))";
char delimChar = ';';
string[] split = null;
string strTemp = sr.ReadLine();
while (strTemp != null)
{
split = strTemp.Split(delimChar);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@ProjectID", SqlDbType.Int).Value = split[0];
cmd.Parameters.Add("@ProjectType", SqlDbType.VarChar).Value = split[1];
cmd.Parameters.Add("@TargetDo", SqlDbType.VarChar).Value = split[2];
//string d = split[3];
cmd.Parameters.Add("@d", SqlDbType.Text).Value = split[3];
cmd.Connection.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
Console.WriteLine(ex.Message);
}
con.Close();
strTemp = sr.ReadLine();
}
sr.Close();
}
这是报错信息 ,上网查了下,是数据类型不匹配的原因,麻烦大神们指点解决方法。