【问题标题】:Format of the initialization string does not conform to specification starting at index 73初始化字符串的格式不符合从索引 73 开始的规范
【发布时间】:2015-01-21 12:24:08
【问题描述】:

我用 sql db 在 c# 中创建了一个应用程序,当我在应用程序中保存数据时,在“system.argumentException”中显示错误“初始化字符串的格式不符合从索引 73 开始的规范”

我使用 windows autentication 和此代码进行连接:

   public static SqlConnection ObtenerConexion() 
   {
       SqlConnection Conn = new SqlConnection("Data source=VICTOR-PC; Initial Catalog=KAZIIM;  Integrated Security=True;");
       Conn.Open();


       return Conn;

   }
}

保存数据按钮的代码是:

 private void btnGuardar_Click(object sender, EventArgs e)
    {
        Cliente Cliente = new Cliente();
        Cliente.ID_C = int.Parse(txtID.Text);
        Cliente.NOMBRES = txtNombres.Text;
        Cliente.CONTACTO = txtCorreo.Text;
        Cliente.CALLE = txtCalle.Text;
        Cliente.NUMERO = int.Parse(txtNumero.Text);
        Cliente.COLONIA = txtColonia.Text;
        Cliente.FECHA_ALTA = txtFecha.Text;

        int resultado = ClienteDAL.Agregar(Cliente);

        if (resultado > 0)
        {
            MessageBox.Show("Datos guardados con exito", "Datos Guardado", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("No se grabaron los datos", "Error al guardar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

异常详情是:

System.ArgumentException was unhandled

HResult=-2147024809 Message=El formato de la cadena de inicialización no se ajusta a la especificación que comienza en el índice 73。 源=系统.数据 堆栈跟踪: en System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) en System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) en System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) zh System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) en System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) en System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) zh System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey 键) zh System.Data.SqlClient.SqlConnection.set_ConnectionString(字符串值) zh System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential) zh System.Data.SqlClient.SqlConnection..ctor(String connectionString) zh app_topico.Kaziim.ObtenerConexion() zh C:\Users\VICTOR\documents\visual studio 2010\Projects\app_topico\app_topico\Kaziim.cs:línea 14 en app_topico.ClienteDAL.Agregar(Cliente pCliente) en C:\Users\VICTOR\documents\visual studio 2010\Projects\app_topico\app_topico\ClienteDAL.cs:línea 14 en app_topico.Form1.btnGuardar_Click(Object sender, EventArgs e) en C:\Users\VICTOR\documents\visual studio 2010\Projects\app_topico\app_topico\Form1.cs:línea 35 zh System.Windows.Forms.Control.OnClick(EventArgs e) zh System.Windows.Forms.Button.OnClick(EventArgs e) zh System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) en System.Windows.Forms.Control.WndProc(Message& m) en System.Windows.Forms.ButtonBase.WndProc(Message& m) en System.Windows.Forms.Button.WndProc(Message& m) zh System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) zh System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) zh System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) zh System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg) zh System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) zh System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) zh System.Windows.Forms.Application.Run(Form mainForm) en app_topico.Program.Main() en c:\users\victor\documents\visual studio 2010\Projects\app_topico\app_topico\Program.cs:línea 18 zh System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) zh System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) zh Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() zh System.Threading.ThreadHelper.ThreadStart_Context(对象状态) en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) zh System.Threading.ThreadHelper.ThreadStart() 内部异常:

【问题讨论】:

  • 这个异常到底在哪一行?
  • 嗨!在这个:SqlConnection Conn = new SqlConnection("Data source=VICTOR-PC; Initial Catalog=KAZIIM; Integrated Security=True;");
  • 尝试删除多余的空格和最后一个“;”。该错误表示第 73 个字母有一些错误。会不会有一些字母显示不正确?将您的代码行粘贴到记事本中,然后返回到您的代码中以消除“隐藏”字母。甚至更好。再次写入该行,无需任何复制/粘贴。
  • 感谢您的支持! @Soner Gonu

标签: c# visual-studio-2010 sqlconnection


【解决方案1】:

在连接字符串的最后,;" 字符之间似乎有一个 UTF-8 字节顺序标记,正好在错误所说的字符位置。它是不可见的,但如果将其复制到新的文本文件并使用 UTF-8 编码保存并在十六进制编辑器中检查文件,则可以看到它。只需删除最后几个字符并再次键入即可。

"Data source=VICTOR-PC; Initial Catalog=KAZIIM; Integrated Security=True;<some invisible stuff is here>"

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多