--------------------------------------------------------------------------------
从索引 0 处开始,初始化字符串的格式不符合规范。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentException: 从索引 0 处开始,初始化字符串的格式不符合规范。
源错误:
行 122: {
行 123: string sqlstr = "select * from T_Theater";
行 124: sqlcon = new SqlConnection(strCon);
行 125: SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
行 126: DataSet myds = new DataSet();
----------------------------------------------------------------------------------------
下面是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "MyOfficeConnectionString";
// private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("/GuangDong/App_Data/#HS.mdb");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
if (Fileupload1.HasFile)
{
//获取文件路径
string path = Fileupload1.PostedFile.FileName;
//获取文件名
string FileName = Fileupload1.FileName;
//根据文件结尾名判断文件格式
if (FileName.ToLower().Substring(FileName.Length - 3) != "xls" && FileName.ToLower().Substring(FileName.Length - 4) != "xlsx")
{
Response.Write("<script type='text/JavaScript'>alert('请选择Excel文件!')</script>");
return;
}
//获取Excel的数据datatable
DataTable dt = GetTable(path, FileName);
using (SqlConnection conn = new SqlConnection("server=.;database=Test;uid=sa;pwd=12345678;"))
{
conn.Open();
SqlTransaction sqlTransaction = conn.BeginTransaction();
// 将事务应用于Command
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = conn;
sqlCommand.Transaction = sqlTransaction;
try
{
foreach (DataRow row in dt.Rows)
{
string Theater = row["影院编号"].ToString();
string TheaterName = row["影院名称(艺恩)"].ToString();
string MonthBoxOffice = row["月票房"].ToString();
string Nationalproportionof = row["全国占比(百分比显示)"].ToString();
sqlCommand.CommandText = string.Format("insert into T_Theater([Theater],TheaterName,MonthBoxOffice,Nationalproportionof) values('{0}','{1}','{2}','{3}')", Theater, TheaterName, MonthBoxOffice, Nationalproportionof);//sql数据库
sqlCommand.ExecuteNonQuery();
}
//成功提交
sqlTransaction.Commit();
Response.Write("<script type='text/JavaScript'>alert('已成功导入文件!')</script>");
}
catch (Exception ex)
{
// 出错回滚
sqlTransaction.Rollback();
Response.Write("<script type='text/JavaScript'>alert('导入文件失败!')</script>");
}
finally
{
conn.Close();
}
}
}
else
{
Response.Write("<script type='text/JavaScript'>alert('请选择要导入的文件!')</script>");
}
}
//将Execl的数据读取到Datatable
protected DataTable GetTable(string path, string fileName)
{
string connString = "";
if (fileName.ToLower().IndexOf(".xlsx") < 0)
{
connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;IMEX=1'", path);
}
else
{
connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 12.0;HDR=YES'", path);
}
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString);
string sql = "SELECT * FROM [Sheet1$]";
DataTable dt;
try
{
conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
conn.Close();
dt = ds.Tables[0];
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception err)
{
return null;
}
return dt;
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from T_Theater where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update T_Theater set Theater='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',TheaterName='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',MonthBoxOffice='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Nationalproportionof='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "' where ID='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//绑定
public void bind()
{
string sqlstr = "select * from T_Theater";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "T_Theater");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
5 个解决方案
#1
断点跟踪,是哪句报错?
#2
看那看你getdatatable中的 connString 是什么内容,你这个错误就是连接字符串错误
#3
这是我config中的代码
<configuration>
<connectionStrings>
<add name="MyOfficeConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString2" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString3" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000"/>
<add key="MyOfficeConnectionString" value="server=IT;database=Test;uid=sa;pwd=12345678;"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
<configuration>
<connectionStrings>
<add name="MyOfficeConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString2" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString3" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000"/>
<add key="MyOfficeConnectionString" value="server=IT;database=Test;uid=sa;pwd=12345678;"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
#4
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "MyOfficeConnectionString";
//上面一行修改成
string strCon = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString;
//绑定
public void bind()
{
string sqlstr = "select * from T_Theater";
sqlcon = new SqlConnection( strCon); //你原先的是直接传了一个字符串,而不是数据库链接字符串
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "T_Theater");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "MyOfficeConnectionString";
//上面一行修改成
string strCon = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString;
//绑定
public void bind()
{
string sqlstr = "select * from T_Theater";
sqlcon = new SqlConnection( strCon); //你原先的是直接传了一个字符串,而不是数据库链接字符串
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "T_Theater");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
#5
我还是觉得用断点调试一下吧...
#1
断点跟踪,是哪句报错?
#2
看那看你getdatatable中的 connString 是什么内容,你这个错误就是连接字符串错误
#3
这是我config中的代码
<configuration>
<connectionStrings>
<add name="MyOfficeConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString2" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString3" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000"/>
<add key="MyOfficeConnectionString" value="server=IT;database=Test;uid=sa;pwd=12345678;"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
<configuration>
<connectionStrings>
<add name="MyOfficeConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString2" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
<add name="TestConnectionString3" connectionString="Data Source=.;Initial Catalog=Test;User ID=sa;Password=12345678"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000"/>
<add key="MyOfficeConnectionString" value="server=IT;database=Test;uid=sa;pwd=12345678;"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
#4
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "MyOfficeConnectionString";
//上面一行修改成
string strCon = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString;
//绑定
public void bind()
{
string sqlstr = "select * from T_Theater";
sqlcon = new SqlConnection( strCon); //你原先的是直接传了一个字符串,而不是数据库链接字符串
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "T_Theater");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "MyOfficeConnectionString";
//上面一行修改成
string strCon = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString;
//绑定
public void bind()
{
string sqlstr = "select * from T_Theater";
sqlcon = new SqlConnection( strCon); //你原先的是直接传了一个字符串,而不是数据库链接字符串
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "T_Theater");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
#5
我还是觉得用断点调试一下吧...