不允许从数据类型nvarchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。 [英] Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

查看:156
本文介绍了不允许从数据类型nvarchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.UI.WebControls;

public partial class Medicaldetails : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hospitalConnectionString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindEmployeeDetails();
        }



    }

    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from medicine", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;

            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            con.Close();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }

    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int s_no = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["s_no"].ToString());
        TextBox bill_number = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("bill_number");
        TextBox date = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_date");
        TextBox shop_name = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_shopname");
        TextBox ammount = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_amt");
        con.Open();
        SqlCommand cmd = new SqlCommand("update medicine set  bill_number='" + bill_number.Text + "', date = '" + date.Text + "', shop_name ='" + shop_name.Text + "', ammount = '" + ammount.Text + "'  where  s_no=" + s_no, con);
        cmd.ExecuteNonQuery();
        con.Close();
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int s_no = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["s_no"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from medicine where  s_no  =" + s_no, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();


        }
    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox bill_number = (TextBox)gvDetails.FooterRow.FindControl("txt_bill");
            TextBox date = (TextBox)gvDetails.FooterRow.FindControl("txtDate");
            TextBox shop_name = (TextBox)gvDetails.FooterRow.FindControl("txt_shopname11");
            TextBox Ammount = (TextBox)gvDetails.FooterRow.FindControl("txt_amt11");
            FileUpload fileupd2 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload1_test");
            
            FileUpload fileupd1 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_bill");
            FileUpload fileupd = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_pre");
            con.Open();

            SqlCommand cmd = new SqlCommand("insert into medicine(bill_number, dt, shop_name, Ammount,pres, medicine_bills, test_bills ) values(@bill_number, @dt, @shop_name, @Ammount, @pres, @medicine_bills, @test_bills)", con);
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@bill_number", bill_number.Text);
            cmd.Parameters.AddWithValue("@dt", date.Text);
            cmd.Parameters.AddWithValue("@shop_name", shop_name.Text);
            cmd.Parameters.AddWithValue("@Ammount", Ammount.Text);
            cmd.Parameters.AddWithValue("@pres", fileupd.FileName);
            cmd.Parameters.AddWithValue("@medicine_bills", fileupd1.FileName);
            cmd.Parameters.AddWithValue("@test_bills", fileupd2.FileName);


            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
            }

            else
            {
              
            }
        }
    }
    protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        FileUpload fileupd2 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload1_test");
        FileUpload fileupd1 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_bill");
        FileUpload fileupd = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_pre");
        string filename = Path.GetFileName(fileupd.PostedFile.FileName);
        string filename_bill = Path.GetFileName(fileupd1.PostedFile.FileName);
        string filename_test = Path.GetFileName(fileupd2.PostedFile.FileName);
        Stream str = fileupd.PostedFile.InputStream;
        Stream str1 = fileupd1.PostedFile.InputStream;
        Stream str2 = fileupd2.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(str);
        Byte[] size = br.ReadBytes((int)str.Length);
      
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = " insert into medicine (pres, medicine_bills, test_bills) values(@pres, @medicine_bills, @test_bills)";
       
        cmd.Parameters.AddWithValue("@pres", fileupd.FileName);
        cmd.Parameters.AddWithValue("@medicine_bills", fileupd1.FileName);
        cmd.Parameters.AddWithValue("@test_bills", fileupd2.FileName);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindEmployeeDetails();





            
    }
}



< br $>


表是...








table is...


CREATE TABLE [dbo].[medicine](
    [s_no] [int] IDENTITY(1,1) NOT NULL,
    [bill_number] [int] NULL,
    [dt] [date] NULL,
    [shop_name] [varchar](88) NULL,
    [Ammount] [varchar](88) NULL,
    [medicine_bills] [varbinary](max) NULL,
    [test_bills] [varbinary](max) NULL,
    [pres] [varbinary](max) NULL,

推荐答案

可能你错误地定义了你的表。尝试将字段的数据类型从varbinary更改为varchar,因为它对于那些类型的数据是更相关的数据类型。



Probably you have defined your table incorrectly. Try changing the datatype of fields from varbinary to varchar beacause it is more relevent datatype for those kind of data.

ALTER TABLE [dbo].[medicine]
ALTER COLUMN [medicine_bills] VARCHAR(MAX) NULL

ALTER TABLE [dbo].[medicine]
ALTER COLUMN [test_bills] VARCHAR(MAX) NULL

ALTER TABLE [dbo].[medicine]
ALTER COLUMN [pres] VARCHAR(MAX) NULL





注意:如果要将文件保存到数据库中你需要将它们转换为字节数组,你可以使用现有的表结构保存它们。



希望,它有帮助:)



Note: if you want to save the files in to the database then you need convert them to byte array and you can save them with the existing table structure.

Hope, it helps :)


这篇关于不允许从数据类型nvarchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆