我使用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 消息= El formato de la cadenadeinicializaciónnose ajusta alapecpecificaciónquecomienza enelíndice73。 来源= System.Data 堆栈跟踪: 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 definitions,Boolean firstKey) en System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms definitions,Boolean useOdbcRules) en 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) en System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) en System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) en System.Data.SqlClient.SqlConnection..ctor(String connectionString,SqlCredential credential) en System.Data.SqlClient.SqlConnection..ctor(String connectionString) en app_topico.Kaziim.ObtenerConexion()zh C:\ Users \ VICTOR \ documents \ visual studio 2010 \ Projects \ app_topico \ app_topico \ Kaziim.cs:línea14 en app_topico.ClienteDAL.Agregar(Cliente pCliente)zh C:\ Users \ VICTOR \ documents \ visual studio 2010 \ Projects \ app_topico \ app_topico \ ClienteDAL.cs:línea14 en app_topico.Form1.btnGuardar_Click(Object sender,EventArgs e)zh C:\ Users \ VICTOR \ documents \ visual studio 2010 \ Projects \ app_topico \ app_topico \ Form1.cs:línea35 en System.Windows.Forms.Control.OnClick(EventArgs e) en System.Windows.Forms.Button.OnClick(EventArgs e) en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) en System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32单击) en System.Windows.Forms.Control.WndProc(Message& m) en System.Windows.Forms.ButtonBase.WndProc(Message& m) en System.Windows.Forms.Button.WndProc(Message& m) en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) en 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 reason,ApplicationContext context) en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) en System.Windows.Forms.Application.Run(Form mainForm) en app_topico.Program.Main()c:\ users \ victor \ documents \ visual studio 2010 \ Projects \ app_topico \ app_topico \ Program.cs:línea18 en System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) en System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) cn Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() en System.Threading.ThreadHelper.ThreadStart_Context(Object state) 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) en System.Threading.ThreadHelper.ThreadStart() InnerException:
答案 0 :(得分:0)
在连接字符串的最末端,;
和"
字符之间似乎有一个UTF-8字节顺序标记,正好在错误所在的字符位置。它是不可见的,但如果您将其复制到新的文本文件并使用UTF-8编码保存并在十六进制编辑器中检查该文件,则可以看到它。只需删除最后几个字符并再次输入即可。
"Data source=VICTOR-PC; Initial Catalog=KAZIIM; Integrated Security=True;<some invisible stuff is here>"