今天看啥  ›  专栏  ›  彬sir哥

Android studio数据如何发送IIS服务器保存SQL Server 2014数据库

彬sir哥  · CSDN  ·  · 2020-01-01 00:00

1.Android 访问网络

1.1 Android 自定义如下图:
在这里插入图片描述
1.2 MainActivity.java 中,输入姓名,选择手机一个图片

......
public void BtnEnd(View view){
        if (bitmap != null && !name.getText().toString().equals("")){
            UploadUtil.mBitmap = bitmap;//获取手机图片
            UploadUtil.mName(name.getText().toString());//姓名
        }
    }
......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.3 UploadUtil.java 中,把姓名和图片发送IIS服务器

 if (mBitmap != null)
        {
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] data = baos.toByteArray();
            RequestBody image_data = RequestBody.create(MediaType.parse("application/octet-stream"), data);
            requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("name", name)//姓名
                    .addFormDataPart("image", System.currentTimeMillis() + ".jpg", image_data).build();//图片
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response;
            try {
                response = mOkHttpClient.newCall(request).execute();
                if (response.message().toString().equals("OK")){
                    System.out.print("发送成功");
                }else {
                    System.out.print("发送失败");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

1.4 AppConfig.java 中,设置自己C#的ip地址

public static final String url = "http://xxx.xxx.x.xxx:xxxx/Handler1.ashx";
  • 1
  • 1

2.VS2010 新建项目

2.1 VS2010新建项目如下图:
在这里插入图片描述
2.2 Handler1.ashx.cs 中,这是得到Android姓名和图片的网络请求

using (SqlConnection connection = new SqlConnection("Data Source=xxx.xxx.x.xxx;Initial Catalog=(SQLServer的数据库名 );Integrated Security=True;User Id = (用户名);Password = (密码);"))
                {
                    connection.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = connection;
                    string commandText = "Insert into faceid values (@name,@image)";
                    cmd.CommandText = commandText;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@name", SqlDbType.VarChar);//姓名
                    cmd.Parameters["@name"].Value = name;
                    cmd.Parameters.Add("@image", SqlDbType.VarBinary);//图片
                    cmd.Parameters["@image"].Value = imagetype;
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    connection.Close();
                    context.Response.Write("导入成功");
                }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.3 app.config中,设置数据库名的位置和名称,设置自己的ip地址、用户名和密码

<?xml version="1.0"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="(SQLServer的数据库名)/bin/Release;" />
    </assemblyBinding>
  </runtime>
  <startup  useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <appSettings>
    <add key="ConnectionString" value="Data Source=xxx.xxx.x.xxx;Initial Catalog=(SQLServer的数据库名 );Integrated Security=True;User Id = (用户名);Password = (密码);" />
  </appSettings>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.SQL Server 2014 新建数据库

3.1 SQL Server新建数据库如下图:
在这里插入图片描述

4.android发送成功失败

4.1 UploadUtil.java 中,android发送成功失败

response = mOkHttpClient.newCall(request).execute();
                if (response.message().toString().equals("OK")){
                    System.out.print("发送成功");
                }else {
                    System.out.print("发送失败");
                }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.2 如果android发送成功后输出信息如下图:
在这里插入图片描述

4.android源代码下载:

https://download.csdn.net/download/qq_35091074/19148810

5.C#源代码下载:

https://download.csdn.net/download/qq_35091074/19149119

6.SQL Server 2014的数据库没有下载,建议自己新建数据库




原文地址:访问原文地址
快照地址: 访问文章快照