从代码

时间:2017-04-19 10:54:58

标签: c# sql sql-server dapper

在我的C#app中,我正在使用Dapper调用存储过程:

return conn.Query<MyList>("check_data", new { dataToCheck }, commandType: CommandType.StoredProcedure);

dataToCheckDataTable,因为check_data接受TVP。此表类型有2列(from_date varchar(20),to_date varchar(20))并基于以下类。

public class DataToCheckType
{
    public DateTime? from_date {get; set;}
    public DateTime? to_date {get; set;}
}
然后,

Dapper将以下内容发送到SQL Server:

declare @p1 dbo.CheckDataType
insert into @p1 values('2017-04-19 00:00:00','2017-04-19 00:00:00')

exec check_data @dataToCheck=@p1

SP本身很简单。它调用一个构建SQL语句的函数,然后执行该函数:

DECLARE @sql nvarchar(max) = dbo.GetSql(@dataToCheck);

DECLARE @results TABLE (Id int);
INSERT INTO @results EXECUTE(@sql);

GetSql函数的开头如下:

DECLARE @fromDate datetime;
DECLARE @toDate datetime;

SELECT 
    @fromDate = CONVERT(datetime, from_date, 120),
    @toDate = CONVERT(datetime, to_date, 120),
FROM 
    @dataToCheck;

它继续按预期构建SQL语句。从SP运行T-SQL时一切正常。

但是,当从代码运行相同时,我收到以下错误:

  

check_data:241从字符串

转换日期和/或时间时转换失败

为什么SP在T-SQL中工作,但在从代码调用时却没有?日期有什么问题?

1 个答案:

答案 0 :(得分:0)

通过将表格类型中的列从NullPointerException更改为@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); MenuItem item = menu.findItem(R.id.action_search); searchView = (SearchView) MenuItemCompat.getActionView(item); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(MainActivity.this, query, Toast.LENGTH_LONG).show(); Intent intent = new Intent(MainActivity.this, SearchActivity.class); intent.putExtra("query", query); startActivity(intent); return true; } @Override public boolean onQueryTextChange(String newText) { Log.d("Text Changing", newText); if (newText.length() > 3) { searchView.setSuggestionsAdapter(provideSuggestionAdapter(newText)); } return true; } }); return true; } void jsonParse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); StringBuilder mAuthorBuilder = new StringBuilder(); if (jsonObject != null) { JSONArray itemArray = jsonObject.getJSONArray("items"); for (int i = 0; i < itemArray.length(); i++) { JSONObject itemObject = itemArray.getJSONObject(i); JSONObject volumeInfo = itemObject.getJSONObject("volumeInfo"); if (volumeInfo.has("title")) { mBookNames.add(volumeInfo.get("title").toString()); } if (volumeInfo.has("authors")) { JSONArray authorArray = volumeInfo.getJSONArray("authors"); if (volumeInfo.getJSONArray("authors") != null) { mAuthorBuilder.delete(0,mAuthorBuilder.length()); for (int j = 0; j < authorArray.length(); j++) { mAuthorBuilder.append(authorArray.getString(j) + "\n"); } mAuthors.add(mAuthorBuilder.toString()); } } } } } private SimpleCursorAdapter provideSuggestionAdapter(final String query) { Log.d("start:","pass"); SimpleCursorAdapter a = new SimpleCursorAdapter(this, R.layout.cust_list_suggestions, null, from, to, 0); a.setStringConversionColumn(1); FilterQueryProvider filterQueryProvider = new FilterQueryProvider() { @Override public Cursor runQuery(CharSequence constraint) { constraint = query.trim().replaceAll("\\s+", "+"); Log.d("Query: ", "runQuery constraint: " + constraint); if (constraint == null) { return null; } String[] columnNames = {BaseColumns._ID, "bName", "bAuthor"}; MatrixCursor c = new MatrixCursor(columnNames); try { String urlString = "https://www.googleapis.com/books/v1/volumes?q=" + constraint + "&key=" + getResources().getString(R.string.google_api_key); URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); InputStream is = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder builder = new StringBuilder(); String line; while((line = reader.readLine())!=null){ builder.append(line); } String jsonStr = builder.toString(); Log.d("jsonStr",jsonStr); jsonParse(jsonStr); for (int i=0;i<mAuthors.size();i++){ c.newRow().add(i).add(mBookNames.get(i)).add(mAuthors.get(i)); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return c; } }; a.setFilterQueryProvider(filterQueryProvider); Log.d("end:","pass"); return a; } 来解决问题。