在C#silverlight应用程序中,我有一个datagrid控件,可以从sql数据库表中获取联系人详细信息。我可以通过键入文本框并单击“添加”按钮向数据网格添加新的详细信息。有一个上传按钮上传图像控件中的图像,并正常工作。但我似乎无法为表中的每个联系人保存图像,并使用wcf服务将其与其他信息一起显示。
这是我在上传图片按钮中写的内容:
private void uploadbtn_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG files|*.jpg";
//openFileDialog.Filter = "Images (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == true)
{
Stream stream = (Stream)openFileDialog.File.OpenRead();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
BitmapImage bi = new BitmapImage();
bi.SetSource(stream);
MyImage.Source = bi;
string fileName = openFileDialog.File.Name;
}
}
在我的添加联系人按钮中:
ServiceReference1.Contact contact = new ServiceReference1.Contact();
ServiceReference1.Service1Client connection = new ServiceReference1.Service1Client();
connection.InsertContactCompleted += new EventHandler<ServiceReference1.InsertContactCompletedEventArgs>(connection_InsertContactCompleted);
contact.ConName = txtname.Text;
contact.ConNumber = txtnumber.Text;
contact.ConDescription = txtdescription.Text;
并在服务中:
[OperationContract]
public int InsertContact(Contact cnt)
{
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = " insert into Contact(ConName, ConNumber,ConDescription) values(@1,@2,@3)";
cmd.CommandTimeout = 300;
cmd.Parameters.Add("@1", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConName;
cmd.Parameters.Add("@2", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConNumber;
cmd.Parameters.Add("@3", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConDescription;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return i;
}
else
{
return 0;
}
}
}
}
我不知道如何将图像添加到这些代码中。