iPhone到MS SQL图像数据类型转换问题 [英] iPhone to MS SQL Image Data Type Conversion Question
问题描述
好吧,我们开始吧!
我目前正在开发一个iPhone应用程序,将工作作为在一个教堂事件安全检查系统。其主要目标是要能够搜索pre现有MS SQL数据库的参与者,然后将其保存在iPhone / iPod的触摸/ iPad的列表。很容易的。一个在MS SQL数据库中的字段是图像数据类型的,它的功能是将参加者的图像存储在二进制形式。我目前不能切换到VARBINARY(MAX)。
I am currently developing an iPhone app that will work as a security check-in system for events at a church. The main goal is to be able to search a pre existing MS SQL database for a participant, and then save them to a list on the iPhone/iPod Touch/iPad. Easy enough. One of the fields in the MS SQL database is of Image data type, and its function is to store the participant's image in binary form. I currently am not allowed to switch to varbinary(MAX).
从iPhone,我使用的:
From the iPhone, I use:
UIImage *image = [UIImage imageNamed:@"nameOfImage.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image,1.0);
NSString *imageString = (NSString *)[[[NSString alloc] init] base64StringFromData:(NSData *)imageData];
您可能会想知道base64StringFromData是什么。这是这里讨论的一个类别,任何的base64库上iphone-SDK?
You may be wondering what the base64StringFromData is. It's a category discussed here, Any base64 library on iphone-sdk?
在类别中,它返回一个字符串作为NSASCIIStringEncoding。我然后使用POST发送字符串到Visual Basic的Web服务。
In the category, it returns a string as NSASCIIStringEncoding. I then send that string to a Visual Basic web service using POST.
假设在我已经设置并打开了我的SQL连接,initlialized一个SQL数据适配器,命令如下。因此,功能类似于此:
Assume in the following that I have already set up and opened my SQL connection, and initlialized a SQL Data adapter and command. So the function looks similar to this:
Public Function sendCommand(ByVal image As String) As String
Dim commandString As String
commandString = "insert into phoneApp(image) values(@image)"
Dim encoding As New System.Text.UTF8Encoding
sqlDataAdapter.InsertCommand = (commandString, sqlConnection)
sqlDataAdapter.InsertCommand.Parameters.Add(@"image", SqlDbType.Image, image.length).Value = encoding.GetBytes(image)
sqlDataAdapter.ExecuteNonQuery()
End Function
现在,终于,这里发生了什么。在另一个函数,我叫Response.BinaryWrite(SqlDataReader.Item(图像))。在Internet Explorer中,这样做会则使图像出现在浏览器。相反,它显示以base64形式的字符串。复制和粘贴该字符串中以base64转换器断网,和它的作品。因此,在iPhone上的编码没有连接code正确采用base64。
Now, finally, here's what is happening. In another function, I call Response.BinaryWrite(SqlDataReader.Item("image")). In Internet Explorer, doing this would then make the image appear in the browser. Instead, it displays the string in base64 form. Copy and pasting that string in a base64 converter off the net, and it works. So the encoding on the iPhone did encode to base64 properly.
这里的问题。难道我连需要被编码为Base64保存成图像数据类型,或者我应该重点学习如何连接code NSData的成不同的二进制字符串?
Here's the question. Do I even need to be encoding to base64 to save into Image Data Type, or should I be focusing on learning how to encode the NSData into a different binary string?
推荐答案
我想通了,并会留下我的回答对任何人碰到这个失蹄。祝你好运在你的节目。
I figured it out, and will leave my answer to anyone that stumbles across this. Good luck to you in your programming.
它实际上是比我想象的简单得多。只需使用System.Convert在将InsertCommand字节通过。
It actually was a lot simpler than I thought. Just use System.Convert to pass in the bytes on the InsertCommand.
sqlDataAdapter.InsertCommand = (commandString, sqlConnection)
sqlDataAdapter.InsertCommand.Parameters.Add(@"image", SqlDbType.Image, image.length).Value = System.Convert.FromBase64String(image)
sqlDataAdapter.ExecuteNonQuery()
在本质上,我得到的编码字符串的字节数,但从来没有进一步的解码下来就行了。于是我通过转换它被插入到表之前停止的双重编码。
In essence, I was getting the bytes for the encoding string but never decoding it further down the line. So I stopped the double encoding by using the Convert before it was inserted into the table.
这篇关于iPhone到MS SQL图像数据类型转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!