从数据库中获取的画布的动态高度,宽度和颜色

时间:2017-01-10 05:44:05

标签: html css html5 css3 canvas

<canvas id="myCanvas" width="900" height="50" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>

<script>    
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 150, 50);
</script>

我有一个MSSQL数据库。如何从该数据库中获取并指定高度,宽度和颜色属性。

1 个答案:

答案 0 :(得分:0)

要连接Mysql数据库,请使用 PHP

试试这个我希望这段代码可以帮助你

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT height, width, color FROM divsize";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {    
   //create a Canvas     
        echo '<canvas id="myCanvas" width="'.$row["width"].'" height="'.$row["height"].'" style="border:1px solid '.$row["color"].';">Your browser does not support the HTML5 canvas tag.</canvas><br>';        
    }
} else {    
 //create default canvas
    echo '<canvas id="myCanvas" width="900" height="50" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>';
}
$conn->close();
?>
使用 SQL

ASP.NET C#

<强> C#

try
{
    SqlConnection con = new SqlConnection("Connectionstring");
    string Query = "SELECT height,width,color FROM divsize";

    SqlCommand cmd = new SqlCommand(Query, con);

    var Result = cmd.ExecuteReader();

    if (Result.HasRows)
    {
       while (Result.Read())
       {
           Dynamicdiv.InnerHtml = "<div style='height:" + Result["height"].ToString() + "px;width:" + Result["width"].ToString() + "px;background-color:" + Result["color"].ToString() + ";'></div >";
       }
    }
    else
    {
       Dynamicdiv.InnerHtml = "<div style='height:100px;width:200px;background-color:red;'></div>";
    }
 }
 catch (Exception ex)
 {
    Response.Write(ex.Message);
 }

<强> ASP

<div runat="server" id="Dynamicdiv"></div>