#2
mywisdom882017-04-11 16:44
|
首先源程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace link_database_test
{
class Program
{
static void Main(string[] args)
{
string connectionString = "server=localhost;database=time_temp;uid=sa;pwd=336823aa";
SqlConnection mySqlConnection = new SqlConnection(connectionString);//连接数据库
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
= "SELECT TOP 5 Date/Time,0,1,2" + "FROM time_temp" + "WHERE Date/Time=2000/1/1";
mySqlConnection.Open();
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
mySqlDataReader.Read();
Console.WriteLine("Date is" + mySqlDataReader["Date/Time"]);
Console.WriteLine("00:00时气温为" + mySqlDataReader["0"]);
Console.WriteLine("01:00时气温为" + mySqlDataReader["1"]);
Console.WriteLine("02:00时气温为" + mySqlDataReader["2"]);
Console.ReadLine();
mySqlDataReader.Close();
mySqlConnection.Close();
}
}
}
这个问题是出现在连接数据库之后的,错误提示出现在SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();此条命令上。
提示信息如下:
未处理System.Data.SqlClient.SqlException
HResult=-2146232060
Message=“/”附近有语法错误。
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=15
LineNumber=1
Number=102
Procedure=""
Server=localhost
State=1
StackTrace:
在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
在 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
在 System.Data.SqlClient.SqlDataReader.get_MetaData()
在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteReader()
在 link_database_test.Program.Main(String[] args) 位置 e:\Master\tunnel project\temp database\link_database_test\link_database_test\Program.cs:行号 22
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
请哪位大神帮忙看看问题出在哪里。
我也曾经更换过读取数据库的命令,但错误提示还依旧存在。我分析应该不是数据库的问题,因为可以连接成功。