2014-09-02 92 views
-1

我在我的表位字段,可以存儲0或1如何將位值從前端存儲到sql server數據庫?

在我的網頁上,我宣佈類似

<label>Do we have your permission to send your cell a text</label> 
<div> 
    <label> 
    <input type="radio" name="ctrluuSendSMS" id="ctrluuSendSMS" value="1">Yes</label> 
</div> 
<div> 
    <label> 
    <input type="radio" name="ctrluuSendSMS" id="ctrluuSendSMS" value="0">No</label> 
</div> 

而我提出我得到了以下錯誤的形式:

String was not recognized as a valid Boolean 

請幫我如何聲明和如何存儲到數據庫

+3

顯示的HTML並不能幫助我們瞭解正在執行插入。顯示該代碼。 – alroc 2014-09-02 18:37:47

回答

0

我相信你應該嘗試的位值傳遞給數據庫爲int,不是字符串(在代碼中轉換,然後作爲整數參數傳遞給StoredProc/ORM)。

(如alroc評論,可我們看到的SQL代碼?)

我假設你的表看起來像

Create Table MyTable 
(
SomePK int not null identity(1,1), 
MyBitValue bit 
) 

所以你插入/更新語句將看起來像

Declare @MyParam int = 1 -- where 1(or 0) is an integer, not a string 
Declare @MyParam2 bit = 1 
Insert Into MyTable(MyBitValue)Values(@MyParam) 
Insert Into MyTable(MyBitValue)Values(@MyParam2) 

Select * From MyTable 

Set @MyParam = 0 
Set @MyParam2 = 0 
Update MyTable Set MyBitValue = @MyParam Where SomePK = 1 
Update MyTable Set MyBitValue = @MyParam2 Where SomePK = 2 

Select * from MyTable 

編輯:我已更新我的示例以顯示同時使用int和位

+1

爲什麼在sql有一個位類型時將它作爲int傳遞? – JonH 2014-09-02 18:52:10

+0

你可以做到這一點,這將是完全有效的。我一直使用int(將C#中的布爾轉換爲int)。更新答案w /位 – 2014-09-02 19:00:14

0

對於Input元素的值屬性,使用「True」和「False」而不是「1」和「0」。

<label>Do we have your permission to send your cell a text</label> 
<div> 
    <label> 
    <input type="radio" name="ctrluuSendSMS" id="ctrluuSendSMS" value="True">Yes</label> 
</div> 
<div> 
    <label> 
    <input type="radio" name="ctrluuSendSMS" id="ctrluuSendSMS" value="False">No</label> 
</div> 

數字字符串必須被轉換爲布爾之前轉換爲數值

Convert.ToBoolean(Convert.ToByte("1")) = True 

但「真」與「假」,將直接轉換。

Convert.ToBoolean("True") = True 

http://msdn.microsoft.com/en-us/library/wh2c31dd(v=vs.110).aspx