那個\0很重要。
using System.Runtime.InteropServices; public bool DoDBConnect(string strDSN,string strUID,string strPWD) { try { strConn = "DSN=" + strDSN + ";UID=" + strUID + ";PWD=" + strPWD + ";"; cn = new OdbcConnection(strConn); cmd = new OdbcCommand(); adp = new OdbcDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = cn; } catch { return false; } return true; } [ DllImport("ODBCCP32.dll") ] private static extern bool SQLConfigDataSource(IntPtr hwndParent, int fRequest, string lpszDriver, string lpszAttributes); public bool CreateODBCDSN(string strDSN, string strServer, string strDB, string strDescription) { string strSQL = "SERVER=" + strServer + "\0"; strSQL += "DESCRIPTION=" + strDescription + "\0"; strSQL += "DSN=" + strDSN + "\0"; strSQL += "DATABASE=" + strDB + "\0"; strSQL += "trusted_connection=no\0"; return SQLConfigDataSource((IntPtr)0, 4, "SQL Server", strSQL); }